I am in a project which uses 3 servers to serve a Magento 2 site. I have written a code to clear cache and re-indexing. But for certain reasons the cache flush has to happen in all the three servers. The reason i am looking for these approach because after every csv product update we need to manually run all these commands. So, I am looking for a programmatic approach by which i can trigger the re-indexing and cache flush commands after CSV product upload.

 * Reindex the indexes and perform chache flush
 namespace Retailinsights\Testing\Controller\Index;

 class Test extends \Magento\Framework\App\Action\Action {

  protected $_indexerFactory;

  public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
    \Magento\Indexer\Model\IndexerFactory $indexerFactory
  ) {
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
    $this->_indexerFactory = $indexerFactory;

  public function reindexAll() {

    /* reindexs the following category */
    $indexer_ids = [

    foreach ($indexer_ids as $indexerId) {

        $indexerFactoryObject = $this->_indexerFactory->create();

        // destroy object
    return $this;

  public function cacheFlush() {

    $typeCollection = $this->_cacheTypeList->getTypes();

    foreach ($typeCollection as $type) {

    foreach ($this->_cacheFrontendPool as $cacheFrontend) {

  public function execute() {

     *  reindex 
     *  Cache Flush

  • you can create root file so after csv import just hit the url in browser – Sourav Jan 2 at 6:29

Create reindex_cache.php file into magento root directory and copy below code into that file.

Url: After csv import just hit the domain.com/reindex_cache.php in browser.

    try {
        require __DIR__ . '/app/bootstrap.php';
    } catch (\Exception $e) {
        echo <<<HTML
    <div style="font:12px/1.35em arial, helvetica, sans-serif;">
        <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
            <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
            Autoload error</h3>

    ini_set('max_execution_time', 0);
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $objectManager = $objectManager;

    // set whether frontend or admin
    $state = $objectManager->get('Magento\Framework\App\State');

    // code for reindexing 
    $indexerCollectionFactory = $objectManager->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
    $indexerFactory = $objectManager->get("\Magento\Indexer\Model\IndexerFactory");

    $indexerCollection = $indexerCollectionFactory->create();
    $allIds = $indexerCollection->getAllIds();
    foreach ($allIds as $id) {
        $indexer = $indexerFactory->create()->load($id);

// code for caching

    $_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) {
    foreach ($_cacheFrontendPool as $cacheFrontend) {
}catch(Exception $e){


  • Hi Sourav, If you closely look at my code above, i have implemented the same code as you have specified. Adding to that i was this code to get executed in all the 3 servers from one php file. – kaushik kumar roy Jan 2 at 9:10
  • It may be risk to clean and re-index from database level. – Sourav Jan 3 at 6:09
  • Please mark my answer so other take benefit from this. – Sourav Jan 3 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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