Tutorial “flush cache and reindexing programatically in magento 2”, which will be help you to flush cache and reindexing magento 2 programatically . I have also posted same article for magento 1 reindexing-magento-programmatically .

1. Reindex:
1.1.By CMD:
1 2 |
php bin/magento indexer:reindex //for all php bin/magento indexer:reindex catalog_category_product //for specific id |
1.2.Programatically:
1.2.1.By Using DI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * @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 } } |
1.2.2.By using OM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$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 } } |
2.Cache flush/Clean:
2.1.CMD:
1 2 |
php bin/magento cache:clean php bin/magento cache:flush |
2.2.Programmatically:
2.2.1.Using DI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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(); } |
2.2.2.Using OM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$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); }is foreach ($_cacheFrontendPool as $cacheFrontend) { $cacheFrontend->getBackend()->clean(); } }catch(Exception $e){ echo $msg = 'Error : '.$e->getMessage();die(); } |
Hope you like this article “flush cache and reindexing programatically in magento 2” and will never to share for others.
flush cache and reindexing programatically in magento 2