How To

How to perform Reindexing Programmatically in Magento 2

Indexing is all about how Magento alters data, such as products and categories, to enhance the performance of your store front-end. Once you make any changes to backend or code all your data must be refreshed or updated. Because Magento owns very complex database architecture that contains many database tables to store information like catalog data, prices, users, and stores and to get an instant effect in frontend one must have to reindex all this information in special database tables using indexers. Even making a small change in price or URL requires reindexing of data to get an effect. Instead of manually indexing, using a small piece of code in an extension helps you to perform reindexing programmatically in Magento 2. Here is how to do it.

<?php
namespace Vendor\Extension\Controller;
 
class Myactions 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;
 }
 
   public function Mycustomaction()
   {
   $indexerCollection = $this->indexCollection->create();
   $indexids = $indexerCollection->getAllIds();
 
   foreach ($indexids as $indexid)
   {
      $indexidarray = $this->indexFactory->create()->load($indexid);
 
      //If you want reindex all use this code.
         $indexidarray->reindexAll($indexid);
 
      //If you want to reindex one by one, use this code
         $indexidarray->reindexRow($indexid);
   }
   }
}

That’s it! You can also read our blog to know “How to clean and flush cache programmatically in Magento 2” we have a blog for that too. That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.

Happy Indexing!

Click to rate this post!
[Total: 6 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago