2

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

4 Answers 4

6

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

for more details: flush cache and reindexing programatically in magento 2

1
4

Every CLI command directly maps to a command class in the Magento source code. That makes it pretty easy to track down how Magento is doing it--just search for the command.

cache:flush

Magento\Backend\Console\Command\CacheFlushCommand calls:

    $this->eventManager->dispatch('adminhtml_cache_flush_all');
    $this->cacheManager->flush($cacheTypes);

$this->cacheManager is Magento\Framework\App\Cache\Manager
$this->eventManager is Magento\Framework\Event\ManagerInterface

...so you can inject those two classes into yours, and then call the methods.

indexer:reindex

Magento\Indexer\Console\Command\IndexerReindexCommand calls equivalent to:

    $indexers = $this->indexerCollectionFactory->create()->getItems();
    foreach ($indexers as $indexer) {
        $indexer->reindexAll();
    }

...with $this->indexerCollectionFactory being an instance of Magento\Indexer\Model\Indexer\CollectionFactory.

0

I believe to flush the cache you should be able to do one of these

Mage::app()->cleanCache()

or

Mage::app()->getCacheInstance()->flush(); 

To programmatically reindex you need to initialize the index factory then iterate through each index.

/**
* @var \Magento\Indexer\Model\IndexerFactory
*/
protected $indexerFactory;

public function __construct(
    \Magento\Indexer\Model\IndexerFactory $indexerFactory
) {
    $this->indexerFactory = $indexerFactory;
}

Then you require a function to go through each indexID in the array

foreach ($indexerIds as $indexerId) {
    $indexer = $this->indexerFactory->create();
    $indexer->load($indexerId);
    $indexer->reindexAll();
}

There is a module there that may be of use if you don't want to create your own: https://github.com/sohelrana09/magento2-module-product-indexer

0

Please run the below code from magento root folder under any file name.

Ex - yourmagentorootpath/filname.php

<?php
    $command = 'php bin/magento indexer:reindex && php bin/magento cache:flush';
    echo '<pre>' . shell_exec($command) . '</pre>';
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.