Magento 2 reindex: Definition and how to implement it

Magento 2 reindex is a helpful way to notify bots of the search engines about your new or updated information on your online store. Also, it helps to ensure that your users will always see the latest updates on your Magento site.  Magento has very complex database architecture that includes many database tables to store information like catalog data, prices, users, and stores. So, to have an instant effect in frontend, the admin store need to reindex all this information in special database tables using indexers. Even having a small change in price or URL , the admin requires reindexing of data to get an effect. In this article, we will show you three methods  you can use to reindex in Magento

What Is Magento 2 Reindex?

Reindex Magento 2 is the best solution of transforming data on the front end so that visitors can see it if there is any modification or changes on the Magento 2 store. As you know that, Magento 2 owns a lot of merchant data, which should be accessed, searched and used – catalogs, prices, stores ..etc. They are scattered across different tables.  so, to enhance performance,  Magento 2 offers they all together in special tables called  indexers

Indexers also includes data, which usually are calculated from other properties – for instance, prices. It enables Magento 2 to render catalog pages very fast, without requiring for recalculating prices on-the-fly. However, if you adjust price directly in product, changes will not be showcased because indexer will still includes old recalculated data.  To make changes apply, you need to reindex

When Is Magento 2 Reindex Required?

In Magento 2, there are some indexers required, when any changes are made.

  • Category products;
  • Product categories;
  • Product price;
  • Product entity attribute value;
  • Stock;
  • Catalog rule product;
  • Catalog product rule;
  • Catalog search.

An example of reindexing in Magento 2 is, when changing a product price, the data should be reindexed to be correctly showcased on the frontend. So,  “reindex the indexation” would adequately cut down time spent on calculating a final order price containing cart price rules, bundle pricing, discounts, tier pricing, etc.

How to perform Reindexing in Magento 2

If you does not perform Magento 2 reindexing when make changes in your Magento 2 store, your site would take longer to load and possibly annoying the users and abandoning your store.  And if you do not want this to happen, use 3 below  ways to do it:

Method 1:  Reindex Magento 2 From Admin Panel

Reindexing of data from the admin panel with Magento 2 backend is a great way. Let’s take a look at the below steps to know more.

  • Move to Magento 2 Admin Pane
  • At admin panel, move to System > Tools > Index Management. To the right side, the Index Management grid is show you how you can check indexers status.
  • Select the checkbox next to each type of indexer to change.
  • From the action list, you click the indexing mode.
  • Lastly, you click on the Submit button to apply the choosen mode to indexers.

Method 2: Reindex data by the command line

The second method is following the command line to reindex data. Firstly, let’s me show you reindex commands in Magento 2.

indexer                                                                                                                     
 indexer:info                              Shows allowed Indexers                                                           
 indexer:reindex                           Reindexes Data                                                                   
 indexer:reset                             Resets indexer status to invalid                                                 
 indexer:set-mode                          Sets index mode type                                                             
 indexer:show-mode                         Shows Index Mode                                                                 
 indexer:status                            Shows status of Indexer 
Run Reindex Command Line

Now, I will teach you to use reindex command line easily. After moving to Magento root folder, you can follow this command:

php bin/magento indexer:reinde

Result:

Design Config Grid index has been rebuilt successfully in 00:00:00
Customer Grid index has been rebuilt successfully in 00:00:00
Category Products index has been rebuilt successfully in 00:00:00
Product Categories index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:00
Product EAV index has been rebuilt successfully in 00:00:01
Catalog Search index has been rebuilt successfully in 00:00:01
Stock index has been rebuilt successfully in 00:00:00
Catalog Rule Product index has been rebuilt successfully in 00:00:00
Catalog Product Rule index has been rebuilt successfully in 00:00:00

No, you check indexer status by the below command

php bin/magento indexer:info

And result:

design_config_grid                       Design Config Grid
customer_grid                            Customer Grid
catalog_category_product                 Category Products
catalog_product_category                 Product Categories
catalog_product_price                    Product Price
catalog_product_attribute                Product EAV
catalogsearch_fulltext                   Catalog Search
cataloginventory_stock                   Stock
catalogrule_rule                         Catalog Rule Product
catalogrule_product                      Catalog Product Rule
Reset indexer status to invalid
php bin/magento indexer:reset
Show Index Mode
php bin/magento indexer:show-mode

We will see result:

$ php bin/magento indexer:show-mode

Design Config Grid:                                Update on Save
Customer Grid:                                     Update on Save
Category Products:                                 Update on Save
Product Categories:                                Update on Save
Product Price:                                     Update on Save
Product EAV:                                       Update on Save
Catalog Search:                                    Update on Save
Stock:                                             Update on Save
Catalog Rule Product:                              Update on Save
Catalog Product Rule:                              Update on Save
Set index mode type
php bin/magento indexer:set-mode {realtime|schedule} [indexer]

E.g:

magento indexer:set-mode schedule catalog_category_product catalog_product_category

Method 3: Reindexing Programmatically in Magento 2

Instead of manually indexing, you also run a small piece of code in an extension helps you to perform reindexing programmatically in Magento 2.

<?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 all about how to reindex in Magento 2. With the help of this, the store owner can manage their Magento 2 store more efficiently and bring satisfaction to their potential customers. If you still find it difficult to perform reindexing in Magento 2 or any further questions regarding Magento, then you can get in touch with Magento experts for any free support.

Magesolution is one of the leading Ecommerce Development Company in the world. With experienced Magento developers, we commit to provide high-quality products and services to help customers achieve their business success  CONTACT NOW to let us know your problems. we will make sure to solve your query

Besides, if you are looking out for a cost effective Magento package for your eCommerce store, then look nowhere other than Magesolution. We not only offer an affordable Magento Development Package for all size and budget but also ensure that it helps your online business grow and sustain.

magesolution