---
title: "How to perform Reindexing Programmatically in Magento 2"
url: "https://magecomp.com/blog/how-to-perform-reindexing-programmatically-in-magento-2/"
date: "2019-03-08T18:10:12+00:00"
modified: "2025-02-21T09:54:55+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 271
reading_time: "2 min read"
summary: "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 b..."
description: "Indexing is all about how Magento alters data, even making a small change in price or URL requires reindexing of data to get an effect. Instead of manually i..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Get Instagram Client ID and Client Secret for Magento Instagram Extension"
    url: "https://magecomp.com/blog/get-instagram-client-id-client-secret-magento-instagram-extension/"
  - title: "How to Add a Custom Field to the Cart Price Rules Form in Magento 2?"
    url: "https://magecomp.com/blog/add-custom-field-to-cart-price-rules-form-magento-2/"
  - title: "How to Get Shipment Information Using SOAP API in Magento 2"
    url: "https://magecomp.com/blog/get-shipment-information-using-soap-api-magento-2/"
---

# How to perform Reindexing Programmatically in Magento 2

_Published: March 8, 2019_  
_Author: Dhiren Vasoya_  

![How to perform reindexing programmatically in Magento 2](https://magecomp.com/blog/wp-content/uploads/2019/03/How-to-perform-reindexing-programmatically-in-Magento-2-1024x332.webp)

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.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-5.webp)](https://magecomp.com/magento-malware-removal.html)

Here is how to do it.

```
<?php
namespace VendorExtensionController;

class Myactions extends MagentoFrameworkAppActionAction
{
	protected $indexFactory;
   protected $indexCollection;

   public function __construct(
    MagentoIndexerModelIndexerFactory $indexFactory,
	MagentoIndexerModelIndexerCollectionFactory $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);
  	}
   }
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

That’s it! You can also read our blog to know “**[How to clean and flush cache programmatically in Magento 2](https://magecomp.com/blog/how-to-clean-and-flush-cache-programmatically-in-magento2/)**” 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!

[![Quick Cache and Reindex](https://magecomp.com/blog/wp-content/uploads/2020/05/Quick-Cache-and-Reindex.png)](https://magecomp.com/magento-2-admin-reindex-and-quick-cache.html?utm_source=blog&utm_medium=CTA&utm_campaign=cacheandreindex)


---

_View the original post at: [https://magecomp.com/blog/how-to-perform-reindexing-programmatically-in-magento-2/](https://magecomp.com/blog/how-to-perform-reindexing-programmatically-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-27 13:46:44 UTC_  
