Site: US UK AU |
Nexcess Blog

Clearing the Cache in Magento

May 21, 2011 8 Comments RSS Feed

Clearing the Cache in Magento
“What is the difference between the “Flush Magento Cache” and “Flush Cache Storage” buttons?”
Flush Magento Cache and Flush Cache Storage

We get this question often enough that it’s worth making a short blog post about it. In order to understand the difference between these two options, it’s important to know a little about how caching works in Magento. Specifically the concepts of ids and tagging.

Essentially, an “id” in terms of caching is simply a unique string used to identify cache records in a shared storage. A cache “tag”, is another string that’s used to classify different types of data your application is caching. In Magento, tags differentiate between the following Cache types:

  • Configuration (non-layout XML files)
  • Layouts (all those XML files under app/design/…)
  • Blocks HTML output (Page blocks like headers, footers and callouts)
  • Translations
  • Collections Data
  • EAV types and attributes (reduces some database lookups)
  • Web Services Configuration

Let’s take a look at a list of cache files from a default Magento store for an illustration:

$ ls var/cache/mage--0

As you can see, there’s a distinct pattern to the cache file names. Magento’s implementation of the Zend Cache prefixes all files with “mage”, followed by an id prefix (technically: the first 3 characters from an md5 hash of the path to your app/etc/ directory), followed by the cache Tag and some other identifiers. The whole thing makes up the unique id for the specific cache entry.

With this information at hand, we can better understand what happens when you clear the Magento cache in the back end.

“Flush Magento Cache”

When you click “Flush Magento Cache”, the Cache Controller calls the “flushSystemAction()” method. This function, in turn, calls the “cleanCache()” method of the Mage_Core_Model_App object, which subsequently calls the “clean($tags)” method in the Mage_Core_Model_Cache object. Here’s where the differences between the two methods come into play. Let’s take a look at that function definition:

* Clean cached data by specific tag
* @param   array $tags
* @return  bool
public function clean($tags=array())
if (!empty($tags)) {
if (!is_array($tags)) {
$tags = array($tags);
$res = $this->_frontend->clean($mode, $this->_tags($tags));
} else {
$res = $this->_frontend->clean($mode, array(Mage_Core_Model_App::CACHE_TAG));
$res = $res && $this->_frontend->clean($mode, array(Mage_Core_Model_Config::CACHE_TAG));
return $res;

This method is calling on the Zend Cache object, which we’ll look at in a moment. But first I want to point out that this method is cleaning all cache files matching an array of tags, or, if empty, the tags defined in Mage_Core_Model_App and Mage_Core_Model_Config. This does not clean the entire cache!

Before we look at the Zend_Cache “clean()” function definition, let’s see what happens when we click the other button, “Flush Cache Storage”.

“Flush Cache Storage”

When you click this button, the Cache Controller calls the “flushAllAction()” method, rather than the “flushSystemAction()” method we saw above. This method, in turn, gets the Cache Object from Mage_Core_Model_App similar to the above method, but instead of calling “clean($tags)” on the Cache Object, it calls “flush()”. Let’s look at that definition briefly:

* Clean cached data by specific tag
* @return  bool
public function flush()
$res = $this->_frontend->clean();
return $res;

Well, look here! This method is calling the same Zend Cache method as the method above, except for one major difference: it’s not passing any tags!. Now let’s take a look at the Zend_Cache_Core::clean() method to see what happens when you don’t tell it what to flush:

* Clean cache entries
* Available modes are :
* 'all' (default)  => remove all cache entries ($tags is not used)
* 'old'            => remove too old cache entries ($tags is not used)
* 'matchingTag'    => remove cache entries matching all given tags
*                     ($tags can be an array of strings or a single string)
* 'notMatchingTag' => remove cache entries not matching one of the given tags
*                     ($tags can be an array of strings or a single string)
* 'matchingAnyTag' => remove cache entries matching any given tags
*                     ($tags can be an array of strings or a single string)
* @param  string       $mode
* @param  array|string $tags
* @throws Zend_Cache_Exception
* @return boolean True if ok
public function clean($mode = 'all', $tags = array())
if (!$this->_options['caching']) {
return true;
if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
Zend_Cache::throwException('Invalid cleaning mode');
return $this->_backend->clean($mode, $tags);

The documentation in this function definition is pretty good. You can see here that while the first button – “Flush Magento Cache” – tries to invalidate all cached assets matching any tag passed in the $tags array, “Any” here is but a limited subset of the items stored in the cache. However, when you call the “clean()” method without any parameters, as the “Flush Cache Storage” button does, the default behavior is to remove all cache entries irrespective of tags.

This may seem trivial to those of you using the default filesystem cache with Magento. You can just go in and manually “rm -rf var/cache/*” to clear the cache out. But those of you using the alternate cache types (xcache, memcached, apc, db, sqlite), the “Flush Magento Cache” may not be doing what you want it to do. So, when all else fails and you want to be sure the cache is totally clear, be sure to click on “Flush Cache Storage”.

One caveat before I say farewell, if you are using one of the shared storage cache types – for example two different apps using the same memcached instance – clicking on “Flush Cache Storage” might remove the cache entries for that other application as well. This may not be what you want, so just take heed.

Posted in: Magento