How to process Magento indexes from the command line

Magento is a pretty powerful and complex system. It allows to work with massive amounts of data, but when database is overloaded with tons of records it becomes heavy and slow. Magento uses indexes to solve this problem. Indexes are additional database tables with some flat data, which allows to organize fast responses from the database. By default, core system updates indexes on each item’s save. But in some cases you need to do it manually, for example some types of mass actions etc.

You can update indexes any time from the admin backend (Admin->System->Index Management). But sometimes it causes problems. For example, if you have 10k+ products and a lot of categories, rebuilding ‘catalog url rewrite‘ index may take hours. Then php script can just break because of max_execution_time exceeding. There is a way to solve several problems by running reindex process from the command line.

Providing that you have a SSH access to your hosting server, Magento has a few php scripts that you can run from the command line. PHP scripts, executed by CLI (Command Line Interface) have no restrictions for execution time and that can save your time and nerves. The scripts we will talk about are stored in [MAGENTO_ROOT]/shell/ directory.

Just login with your SSH account and go to the shell scripts folder

cd [MAGENTO_ROOT]/shell/

You should replace [MAGENTO_ROOT] with your own absolute path to Magento root folder. For example /home/Thief/public_html/
Then list all files to see what we have here. Just enter simple command:

ls -l

If you can find something like ‘indexer.php‘, that’s what we need and we can go on. To see the status of all indexes, you can execute the command

php indexer.php --status

The output should look like this:

Terminal Screenshot with Indexes status

To run index rebuild process you can run the next command:

php indexer.php --reindex catalog_product_price

Here catalog_product_price is the index type that you want to rebuild. You can use next arguments for different types of indexes:

catalog_product_attribute Product Attributes
catalog_product_price Product Prices
catalog_url Catalog Url Rewrites
catalog_product_flat Product Flat Data
catalog_category_flat Category Flat Data
catalog_category_product Category Products
catalogsearch_fulltext Catalog Search Index
cataloginventory_stock Stock status

For example if you want to process url rewrites index you should use the following command:

php indexer.php --reindex catalog_url

Background reindex process

Sometimes you may want to run this process in background so you can turn off your computer or just have a couple of beers. In this case, you can use a command line tool, called ‘nohup’. It allows you to run almost any CLI command in background independently from your current session.
If you want to reindex something in background, your command should look like this:

nohup php indexer.php --reindex catalog_product_price &

After executing such command, you can safely log out of ssh. You can also login back any time to look if the process was finished. Just go to the shell catalog again and execute command:

cat nohup.out

If you get ‘Product Prices index was rebuilt successfully‘ on your screen, please accept our congratulations. Just remove this file and be happy.

rm nohup.out

If you get an empty line or nothing, you should allow some more time for the process to finish.

Happy coding! :)

You may also want to read: