Our blog

 

Magento Bulk Cache / Index Update Script

If you are doing any kind of bulk updating etc then you also need to make sure that you rebuild all of the caches and indexes after your bulk updates otherwise things can go wrong.

I have just managed to get the following script to work including when flat catalog is enabled (thanks to Nick of Meanbee for the Mage::app() tip).

This script can be called from the browser (I would highly recommend some form of IP restriction or other security) or called from a cron job.

PHP:
  1. <?php
  2. $magePath = '/path/to/Mage.php';
  3. require_once $magePath;
  4. //Varien_Profiler::enable();
  5. //Mage::setIsDeveloperMode(true);
  6. //ini_set('display_errors', 1);
  7. umask(0);
  8.  
  9. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  10.  
  11. echo '<h3>Cleaning overall Cache</h3>';
  12.  
  13. // clean overall cache
  14. Mage::app()->cleanCache();
  15. echo 'done';
  16.  
  17. echo '<h3>Cleaning Catalog Rewrites</h3>';
  18.  
  19. // clear 'refresh_catalog_rewrites':
  20. //Mage::getSingleton('catalog/url')->refreshRewrites();
  21. Mage::getModel('catalog/url')->refreshRewrites();
  22. echo 'Catalog Rewrites was refreshed succesfuly<br>';
  23.  
  24. echo '<h3>Cleaning Image Cache</h3>';
  25.  
  26. //  clear 'clear_images_cache':
  27. Mage::getModel('catalog/product_image')->clearCache();
  28. echo 'Image cache was cleared succesfuly<br>';
  29.  
  30. echo '<h3>Cleaning Layered Navigation</h3>';
  31.  
  32. //  clear 'refresh_layered_navigation':
  33. Mage::getSingleton('catalogindex/indexer')->plainReindex();
  34. echo 'Layered Navigation Indices was refreshed succesfuly<br>';
  35.  
  36. echo '<h3>Clearing out Search Index</h3>';
  37.  
  38. Mage::getModel('catalogsearch/fulltext')->rebuildIndex();
  39. echo 'Search index was rebuilt succesfully<br>';
  40.  
  41. echo '<h3>Rebuilding Flat Category</h3>';
  42. Mage::getResourceSingleton('catalog/category_flat')->rebuild();
  43. echo 'flat category was rebuilt successfully<br>';
  44.  
  45. echo '<h3>Rebuilding Flat Products</h3>';
  46. Mage::getResourceSingleton('catalog/product_flat_indexer')->rebuild();
  47. echo 'flat product was rebuilt successfully<br>';
  48.  
  49. flush();

More...

More Reading:

  • no matching posts found..
6 Comments

admin
May 11th, 2009

if you have any problems with this..

keep an eye on the core_flag table. If a process starts and doesn't stop properly you might get an orphaned flag in this table which will cause you problems..

 

Magento API Calls | Magento News
September 12th, 2009

[...] example check out this bulk cache update script I pulled [...]

 

craig
August 29th, 2010

I tried the above script "Index Update Script"
But I get his error from the browser

PHP:
Warning: require_once(/path/to/Mage.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/41/d293336770/htdocs/magento/cacherebuilder.php on line 8

Fatal error: require_once() [function.require]: Failed opening required '/path/to/Mage.php' (include_path='.:/usr/lib/php5') in /homepages/41/d293336770/htdocs/magento/cacherebuilder.php on line 8

 

admin
August 31st, 2010

you need to change /path/to/Mage.php to be the actual path to the Mage.php file (which is located in the App folder)

 

Christof Coetzee
September 3rd, 2010

How do you guys feel about the speed of Magento cache refreshing?

I've worked on a Magento store with about 24k products, 50k product images and about 300 categories. At times the cache refreshing took about to 40min to complete.

 

admin
September 3rd, 2010

I haven't known it take that long, though with a script like this I would definitely suggest it is something that's done via cron in the middle of the night

 

 

Leave a Reply