How to run reindexing programmatically in magento 2

The blog is about how you can perform reindexing programmatically in Magento 2.

Please review below reindexing code and write code in any class(Helper, Block, Model, Controller) according to your requirements:


 * @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;

public function runReIndexingProcess() {
   $indexerCollection = $this->_indexerCollectionFactory->create();
   $indexIDs = $indexerCollection->getAllIds();
	foreach ($indexIDs as $indexID) {
		$idx = $this->_indexerFactory->create()->load($indexID);
		//$idx->reindexRow($indexID); // you can run specific reindexRow according to your need
		$idx->reindexAll($indexID); // this reindexes all


Then, you need to call function runReIndexingProcess() to perform reindexing.

Related Posts

Leave a Reply

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