Magento – how reindex and flush cache via code in magento 2

cachemagento2reindex

How can reindex and then flush cache magento in code instead command line?

Best Answer

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();
}
Related Topic