How can I reindex magento using by command line.
Kindly provide steps for the same.
Magento 1.x – How to Reindex Using Command Line
command linemagento-1.9reindex
Related Solutions
indexer:reindex
CMD:
php bin/magento indexer:reindex //for all
php bin/magento indexer:reindex catalog_category_product //for specific id
Using DI
/**
* @var \Magento\Indexer\Model\IndexerFactory
*/
protected $_indexerFactory;
/**
* @var \Magento\Indexer\Model\Indexer\CollectionFactory
*/
protected $_indexerCollectionFactory;
public function __construct(
\Magento\Indexer\Model\IndexerFactory $indexerFactory,
\Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory
){
$this->_indexerFactory = $indexerFactory;
$this->_indexerCollectionFactory = $indexerCollectionFactory;
}
// custom function for reindexing
public function reIndexing(){
$indexerCollection = $this->_indexerCollectionFactory->create();
$allIds = $indexerCollection->getAllIds();
foreach ($allIds as $id) {
$indexer = $this->_indexerFactory->create()->load($id);
//$indexer->reindexRow($id); // or you can use reindexRow according to your need
$indexer->reindexAll(); // this reindexes all
}
}
or by using object manager
$obj = \Magento\Framework\App\ObjectManager::getInstance();
$indexerCollectionFactory = $obj->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
$indexerFactory = $obj->get("\Magento\Indexer\Model\IndexerFactory");
// custom function for reindexing
public function reIndexing(){
$indexerCollection = $indexerCollectionFactory->create();
$allIds = $indexerCollection->getAllIds();
foreach ($allIds as $id) {
$indexer = $indexerFactory->create()->load($id);
//$indexer->reindexRow($id); // or you can use reindexRow according to your need
$indexer->reindexAll(); // this reindexes all
}
}
Cache flush/Clean:
CMD:
php bin/magento cache:clean
php bin/magento cache:flush
Using DI:
public function __construct(
Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
parent::__construct($context);
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
}
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
Using ObjectManager:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
try{
$_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface');
$_cacheFrontendPool = $objectManager->create('Magento\Framework\App\Cache\Frontend\Pool');
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$_cacheTypeList->cleanType($type);
}
foreach ($_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
}catch(Exception $e){
echo $msg = 'Error : '.$e->getMessage();die();
}
You seem to be talking about the php bin/magento CLI tool.
This is something that is wonderful and indeed there is one for M1 - however its not built in and you need to add it to your server. Its called Mage Tools (what we call it around the office)
install link:: https://github.com/netz98/n98-magerun
Is that the answer you were looking for?
Best Answer
You should replace [MAGENTO_ROOT] with your own absolute path to Magento root folder. For example /home/Thief/public_html/ Then list all files to see what we have here. Just enter simple command:
If you can find something like ‘indexer.php‘, that’s what we need and we can go on. To see the status of all indexes, you can execute the command
Here catalog_product_price is the index type that you want to rebuild. You can use next arguments for different types of indexes:
For all reindexall :
php indexer.php --reindexall
Background reindex process Sometimes you may want to run this process in background so you can turn off your computer or just have a couple of beers. In this case, you can use a command line tool, called ‘nohup’. It allows you to run almost any CLI command in background independently from your current session. If you want to reindex something in background, your command should look like this:
For more information refer this link