The article “flush cache and reindex 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 . Flushing cache and reindexing programmatically in Magento 2 is pivotal for maintaining peak performance and ensuring that your online store reflects the latest changes promptly. This comprehensive guide explores both command line (CMD) and programmatic approaches to streamline these crucial processes. Kindly go through the example/line of code I have provided in this article

flush cache and reindexing programatically in magento 2
flush cache and reindexing programatically in magento 2

1. Reindex:

1.1.CMD Approach:

The command line interface (CLI) provides a swift method for reindexing in Magento 2. Execute the following commands:

    php bin/magento indexer:reindex //for all
    php bin/magento indexer:reindex catalog_category_product //for specific id

1.2.Programmatic Approaches:

1.2.1.Using Dependency Injection (DI):

Leveraging Dependency Injection involves creating a custom function within your code. Here’s an example 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
    	}
    }

1.2.2.Using Object Manager (OM):

Alternatively, the Object Manager can be employed for programmatic reindexing:

	$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 Approach:

For cache management through the command line, utilize these commands:

php bin/magento cache:clean
php bin/magento cache:flush

2.2.Programmatic Approaches:

2.2.1.Using DI:

Incorporating Dependency Injection enables programmatic cache management:

	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:

The Object Manager approach for cache management is demonstrated below:

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

In conclusion, the implementation of these methods ensures effective cache flushing and reindexing in Magento 2, guaranteeing a smooth and responsive online shopping experience for your customers. Don’t hesitate to share this comprehensive guide with others to enhance their understanding of these essential processes. Hope you like this article “flush cache and reindex programatically in magento 2” and will never forget to share for others.

flush cache and reindexing programatically in magento 2
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *