Magento 2How To GuideJune 6, 2024Simon Walker

How to Reindex Magento 2 Programmatically?

How to Reindex Magento 2 Programmatically?

Before we discuss reindex Magento 2 programmatically, we need to understand what reindexing in Magento 2 means.

What Is Reindexing in Magento 2?

Magento has an efficient but a complex database architecture with lots of tables holding various types of data. It has almost 40 tables for the products and categories alone. Magento database is not how most databases worked in the past i.e. one table containing product information, another containing category information and maybe another one linking these two together.

Instead, Magento splits data up into many sub tables. This means that when some information is updated, all the tables containing that data should be updated simultaneously so that users can have the latest data. To do this, Magento collects all the relevant information and puts them into a selection of smaller tables. This is called indexing.

Indexing improves the performance of the store’s front end. It helps customers quickly retrieve the information on the front end. If you don’t opt to reindex your Magento 2 website from time to time, the store’s performance and overall user experience will be impacted. Now, there are three ways to reindex Magento 2. We are going to focus on how to do it programmatically.

Reindex Magento 2 Programmatically

Reindexing in Magento 2 is normally done automatically. Magento starts a reindex on save actions. You can also manually reindex Magento 2 via command line or schedule it to take place at specific times. In this article, we are going to see how to reindex programmatically. To do this, you can add the following lines of code in any class or extension.


namespace Vendor\ModuleName\Controller;
class Reindex extends \Magento\Framework\App\Action\Action


protected $indexFactory;
protected $indexCollection;

public function __construct(

\Magento\Indexer\Model\IndexerFactory $indexFactory,
\Magento\Indexer\Model\Indexer\CollectionFactory $indexCollection


$this->indexFactory = $indexFactory;
$this->indexCollection = $indexCollection;


// call this function to do reindexing
public function Myreindexer()


$indexerCollection = $this->indexCollection->create();
$indexids = $indexerCollection->getAllIds();

foreach ($indexids as $indexid)


$indexidarray = $this->indexFactory->create()->load($indexid);

//If you want to reindex all.

//If you want to reindex one by one.




This concludes our article on how to reindex Magento 2 programmatically. If you face any issue or have any questions regarding the above code, contact our expert Magento 2 consultants.

Related Articles:

FAQs About Magento 2 Reindexing

What is reindexing in Magento 2?

To reflect the latest changes in Magento, you need to use the reindex feature which refers to updating and rebuilding index tables within your database.

What happens if I don’t reindex my website after making some changes?

If you have made changes to product information such as pricing, these changes won’t be reflected unless you reindex the database. In simpler terms, your users will be browsing an older version of the website. If they proceed to order an item whose price was updated but not reflected on the website, it can lead to all sorts of problems, including poor user experience.

Is there any way to know if the reindexed was successful or whether it encountered any problems?

After reindexing your Magento 2 store, you will get a message “The stock index has been rebuilt successfully" upon successful reindexing.

How often should I reindex my website?

There is no fixed rule regarding as to when you should reindex your website. Based on our experience, the best time to do it is when you have made significant changes to your store.

The reindexing is taking too long. What should I do?

Keep in mind that there is no time period as to how long it will take for a Magento 2 store to reindex. There are various factors which must be considered, including the store’s size.