How can I programmatically flush Magento’s Cache?

The question:

I’m automating release configurations by bootstrapping Mage in several release scripts. At the end of the scripts, I need to flush Magento’s cache.

Is there a way to flush the cache using one of the Mage classes or methods?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Please try the following code to flush the cache programatically




Method 2

If you really wanted to you could also clean just one or more cache types. This is actually how the admin section does it. Under Mage_Adminhtml_CacheController::massRefreshAction

You can see that it loops through all the parameters types and calls the following

$tags = Mage::app()->getCacheInstance()->cleanType($type);
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));

Possible types are as follows:

  1. config
  2. layout
  3. block_html
  4. translate
  5. collections
  6. eav
  7. config_api
  8. config_api2
  9. full_page

And these can be returned by calling Mage::app()->getCacheInstance()->getTypes()

Method 3

A quick external script to clear all cache:


require_once './app/Mage.php';

try {
    $allTypes = Mage::app()->useCache();
    foreach($allTypes as $type => $value) {
        Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
        echo "{$type} </br>";
    echo 'done';
} catch (Exception $e) {
    echo $e->getMessage();

Method 4

Our we could use n98-magerun. Especially since you should never flush the cache during execution of deployment scripts. Also look at the sys:setup:incremental subcommand for more controlled execution of the Magento setup scripts.

Method 5

If you Really need to clear all cache storages means use the following code,


Here Flush function calls the default magento cache functionality, If you need more clarifications means refer the following url.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment