Magento – Separate cron job for reindex all

cron

I've set up the cron job for magento. Do I have to set up a separate cron job for reindex? I'm running Magento 1.9.1

Best Answer

To make separate & custom cron job for re-indexing follow below code/steps.

  • Add cron job into config.xml as below.
  • <crontab> <jobs> <my_cronscript_custom_re_index> <schedule><cron_expr>30 0 * * *</cron_expr></schedule> <run><model>MODULE_NAME/customreindex::runCustomReIndex</model></run> </my_cronscript_custom_re_index> </jobs> </crontab>
  • 30 0 * * * this cron will run every mid-night
  • Cron model file Customreindex.php
  • class MyPackage_MyModule_Model_Customreindex { public function runCustomReIndex() { Mage::app()->getCacheInstance()->flush(); Mage::app()->cleanCache();
    $indexCollection = Mage::getModel('index/process')->getCollection(); foreach ($indexCollection as $index) { $index->reindexAll(); }
    } }
  • Mage::app()->getCacheInstance()->flush(); and Mage::app()->cleanCache(); are to clear cache. You can avoid it if just need to re-index