23

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?

  • 1
    try this: Mage::app()->cleanCache() or Mage::app()->getCacheInstance()->flush(); – Stefan Gregori May 22 '14 at 16:39
25

Please try the following code to flush the cache programatically

Mage::app()->cleanCache()

or

Mage::app()->getCacheInstance()->flush(); 
40

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));
$updatedTypes++;

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()

  • 3
    Having been faffing about with cache clearing programatically this comment is more useful than the selected answer. As it is important to call the dispatchEvent, my Varnish ESI's were not clearing until I added the dispatchEvent call. (Which will of course allow other modules to fire their code accordingly) – Barry Carlyon Feb 9 '15 at 13:25
6

A quick external script to clear all cache:

<?php

require_once './app/Mage.php';
umask(0);
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

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

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.

  • Can you provide some additional insight into your statement "Especially since you should never flush the cache during execution of deployment scripts"? – STW May 22 '14 at 19:32
  • 1
    Magento has a feature that automatically runs upgrades. This is triggered, when the configuration cache has to be rebuilt. Unless you specifically need that feature and carefully code for it, you introduce a parallel process you or your colleagues may not have anticipated. – Melvyn May 23 '14 at 19:08
1

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

Mage::app()->getCacheInstance()->flush();

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

https://stackoverflow.com/questions/15028159/magento-flush-cache-storage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.