Blog

Automatically Run Indexing in Magento2 via Cron

Indexing plays a vital role in Magento2. Magento modifies data such as products, inventory and categories to improve the performance of your store. If the data changes, the changed data must be updated or re-indexed.

Magento’s architecture is very complex and stores a lot of business data (including catalog information, prices, users and stores) in many database tables. To optimize the performance of the store, Magento collects data in special tables with the help of index.

For example, if you change the price of an item from $9.99 to $7.99. Magento needs to reindex the price change to show it in your store.

Without indexing, Magento would have to calculate the price of each product on the fly, taking into account cart price rules, package prices, discounts, tiered prices, etc. The product price would take a long time to load, which could lead to cart abandonment.

To set these options:

  1. Log in to the Admin.
  2. Click System > Tools > Index Management.
  3. Select the checkbox next to each type of indexer to change.
  4. From the Actions list, click the indexing mode.
  5. Click Submit.

You can also reindex from the command line

The following figure shows an example of setting indexers to Update by Schedule:

Changing indexer modes

Instead of manually running indexing, we can run it at perticular time also. To do that, below are the steps:

  1. Login into ssh terminal
  2. Travel to directory where Magento is installed
  3. Run below command
crontab -e

4. Press i button to below lines in the bottom of the file

0 0 * * * /usr/sbin/relax /opt/remi/php74/root/usr/bin/php /home/ae4978f0/584d2fc8d0.nxcli.net/html/bin/magento indexer:reindex


0 8 * * * /usr/sbin/relax /opt/remi/php74/root/usr/bin/php /home/ae4978f0/584d2fc8d0.nxcli.net/html/bin/magento indexer:reindex


0 4 * * * /usr/sbin/relax /opt/remi/php74/root/usr/bin/php /home/ae4978f0/584d2fc8d0.nxcli.net/html/bin/magento indexer:reindex

Press esc and :wq to save and to come out of this file.

These lines are performing indexing at 12am, 8pm and 4pm respectively.

In this way, we can avoid manually running indexing and can use automatic approach to run indexing.

Thank you for reading this article.

I wish you healthy life and keep learning, keep growing.

 

 

One thought on “Automatically Run Indexing in Magento2 via Cron

Leave a Reply

Your email address will not be published. Required fields are marked *