Magento 2: Unlock Reindex Process via Command Line

This article shows how you can unlock the locked reindexing process Magento 2 via command line.

Table of Contents

Problem

Sometimes a scenario comes when the indexing process gets locked at the time of reindexing. When the process or the index type is locked then you cannot reindex that particular locked index type. The reindex will always skip that index type.

For example, let us assume that the Stock index type is locked. Then, when you try to reindex all data, you will get the following output result.


php bin/magento indexer:reindex

Running the above command will output something like this:

Design Config Grid index has been rebuilt successfully in 00:00:04
Customer Grid index has been rebuilt successfully in 00:00:06
Category Products index has been rebuilt successfully in 00:00:02
Product Categories index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:01
Product EAV index has been rebuilt successfully in 00:00:00
Catalog Search index has been rebuilt successfully in 00:00:04
Stock index is locked by another reindex process. Skipping.
Catalog Rule Product index has been rebuilt successfully in 00:00:00
Catalog Product Rule index has been rebuilt successfully in 00:00:00

You can see above, that the Stock process has been skipped.

Solution

Get Index Types Info

First of all, let’s get the information/list of all the index types present in Magento 2. For this, we use the following command:


php bin/magento indexer:info

Running the above command will output something like this:

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

We find that cataloginventory_stock is the code for index type Stock.

Check Status of Index Types

Let’s check the status of all the index types using the following command:


php bin/magento indexer:status

Running the above command will output something like this:

Design Config Grid: Ready
Customer Grid: Ready
Category Products: Ready
Product Categories: Ready
Product Price: Ready
Product EAV: Ready
Catalog Search: Ready
Stock: Processing
Catalog Rule Product: Ready
Catalog Product Rule: Ready

The Processing status of the index type Stock indicates that it is Locked.

Reset Index Types

Solution to this problem is to reset the index types that are locked, i.e. in Processing state.

Reset all index types:


php bin/magento indexer:reset

Reset multiple index types:


php bin/magento indexer:reset IndexType1 IndexType2 IndexType3

In our current case, we have only one index type (cataloginventory_stock) which is locked. So, to unlock it, we have to run the following command:


php bin/magento indexer:reset cataloginventory_stock

Running the above command will output something like this:

Stock indexer has been invalidated.

Now, let’s check the indexer status:


php bin/magento indexer:status

Running the above command will output something like this:

Design Config Grid: Ready
Customer Grid: Ready
Category Products: Ready
Product Categories: Ready
Product Price: Ready
Product EAV: Ready
Catalog Search: Ready
Stock: Reindex required
Catalog Rule Product: Ready
Catalog Product Rule: Ready

As you can see above, the Stock index type has been unlocked. Its status has been changed from “Processing” to “Reindex required”. Now, you can reindex it with the following command:


php bin/magento indexer:reindex cataloginventory_stock

Running the above command will output something like this:

Stock index has been rebuilt successfully in 00:00:04

PS: This is a blog of my answer given on Magento Stack Exchange.

Hope this helps. Thanks.