Magento: Reindex Data Programmatically

This article shows how to reindex Magento Data Programmatically (through code).

You can manually reindex data from System -> Index Management. However, this article is concerned how this can be done through code/programming.

Currently, there are 9 indexes. They are as under (with their respective key number):-

1. Product Attributes
2. Product Prices
3. Catalog URL Rewrites
4. Product Flat Data
5. Category Flat Data
6. Category Products
7. Catalog Search index
8. Tag Aggregation Data
9. Stock Status

So, if you want to reindex “Category Products” only then you can do as follows:-


$process = Mage::getModel('index/process')->load(6);
$process->reindexAll();

If you want to reindex all data then you can loop through these indices and then reindex them:-


for ($i = 1; $i <= 9; $i++) {
    $process = Mage::getModel('index/process')->load($i);
    $process->reindexAll();
}

Hope this helps. Thanks.