Magento 2 Reindex Programmatically

In this article, you going to see discussion regarding Magento 2 Reindex Programmatically in detail, with simple example and step by step.

If you looking for this topic then your right place.

Let’s start with topic of Magento 2 Reindex Programmatically.

It is very simple to do Reindex Programmatically using Cron.

To do this create an PHP file in the CRON folder.

For achieving this create module name VendorName_ModuleName just for referance.

Next step is to create a file for reindexing called Indexer.php file and then call execute() method.

Below is code snippet for re-indexing programmatically

namespace VendorName\ModuleName\PathtoCron;

class Indexer
    public function __construct(
        \Magento\Indexer\Model\Processor $processor
    ) {
        $this->processor = $processor;

    public function execute()
        /* Regenerate indexes for all indexers */

        /* Regenerate indexes for all invalid indexers */

This class play and important role in execution of re-indexing all.

It is because of the using the class file named as processor.

For doing re-index used mainly class file.

Path for file is given below.


There are two way to reindex process.

One which regenerating indexes for all indexers.

Below is code snippet for this.


Another is to regenerating indexes for all invalid indexers.

It is used for regenerating indexes for all invalid indexers.

Below is code snippet for this.


Check the link to do Magento Product Price Reindex Programmatically

Conclusion :

I hope you like this article and if you have any query, please comment below.

Leave a Comment