Tuesday, July 5, 2022
HomeMagentoMagento 2 Reindex Programmatically

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

<?php
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 */
        $this->processor->reindexAll();

        /* Regenerate indexes for all invalid indexers */
        $this->processor->reindexAllInvalid()
    }
}

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.

\Magento\Indexer\Model\Processor

There are two way to reindex process.

One which regenerating indexes for all indexers.

Below is code snippet for this.

$this->processor->reindexAll();

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.

$this->processor->reindexAllInvalid()

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular