Update: If you use a recent version of Magento there might be a better solution here.
If you have been using Magento with a BIG database of products you will soon realize that the built-in “cache management” is quite limited due to the server’s usual time limit on requests. You just can’t refresh your catalog rewrites on 5000 products in less then a good 5 minutes and the other functions might give you the same problems as well.
By extracting the code from the cache management controller I was able to build a custom script that can be called via command line on the server and that will allow you to do a full cache refresh without any kind of time limit or connection timeout issues. The only requirement is shell access to your website files by SSH, Telnet or a terminal of some sort.
Put this file inside /app/ right next to the Mage.php file. To use it, you can simply navigate to the /app folder with your SSH client (or terminal if you run the website on your local PC) do php -f clearcache.php or for fastcgi adepts php-cgi -f clearcache.php
Call the file clearcache.php and you can comment out the parts you don’t need if you only want to refresh one kind of resource (ie: comment out the stock status try/catch block if you don’t want to refresh product stocks).
<?php ini_set('memory_limit', '512M'); require './Mage.php'; Mage::app('admin'); Mage::setIsDeveloperMode(true); if (!Mage::app()->isInstalled()) {    echo "Application is not installed yet, please complete install wizard first.";    exit(1); } // Only for urls // Don't remove this $_SERVER['SCRIPT_FILENAME'] = 'index.php'; try {    // CLEAN CACHE    Mage::app()->cleanCache();    echo 'Cleared all caches' . "\n";    // CATALOG REWRITES    try {        Mage::getSingleton('catalog/url')->refreshRewrites();        echo 'Catalog Rewrites were refreshed successfully' . "\n";    }    catch (Mage_Core_Exception $e) {        echo $e->getMessage() . "\n";    }    catch (Exception $e) {        echo 'Error while refreshed Catalog Rewrites. Please try again later' . "\n";    }    // IMAGE CACHE    try {        Mage::getModel('catalog/product_image')->clearCache();        echo 'Image cache was cleared succesfuly' . "\n";    }    catch (Mage_Core_Exception $e) {        echo $e->getMessage() . "\n";    }    catch (Exception $e) {        echo 'Error while cleared Image cache. Please try again later' . "\n";    }    // LAYERED NAV    try {        $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();        if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {            $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();            $kill->setFlagData($flag->getFlagData())->save();        }        $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();        Mage::getSingleton('catalogindex/indexer')->plainReindex();        echo 'Layered Navigation Indices were refreshed successfully' . "\n";    }    catch (Mage_Core_Exception $e) {        echo $e->getMessage() . "\n";    }    catch (Exception $e) {        echo 'Error while refreshed Layered Navigation Indices. Please try again later' . "\n";    }    // SEARCH INDEX    try {        Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();        echo 'Search Index was rebuilded successfully' . "\n";    }    catch (Mage_Core_Exception $e) {        echo $e->getMessage() . "\n";    }    catch (Exception $e) {        echo 'Error while rebuilded Search Index. Please try again later' . "\n";    }    // STOCK STATUS    try {        Mage::getSingleton('cataloginventory/stock_status')->rebuild();        echo 'CatalogInventory Stock Status was rebuilded successfully' . "\n";    }    catch (Mage_Core_Exception $e) {        echo $e->getMessage() . "\n";    }    catch (Exception $e) {        echo 'Error while rebuilded CatalogInventory Stock Status. Please try again later' . "\n";    }    // CLEAN CACHE    Mage::app()->cleanCache();    echo 'Cleared all caches' . "\n";      echo "\n" . 'Cache clear complete!' . "\n";    exit(0); } catch (Exception $e) {    Mage::printException($e); } exit(1);
2 responses to “Magento: Ultimate cache clear script”
Didn’t work for me. I think Magento has a big issue in their DB. In my opinion they are either missing quite a few indexes on db tables/columns, have too many indexes or both.
I had to start the script with an explicit php5 command:
/usr/local/bin/php5 -f clearcache.php
May it helps someone