Get Free Audit

How to Manage Cache in Magento 2: Clean, Flush, Enable, Disable

Nov 1, 2018

3893 Andrey Litvin

How to Manage Cache in Magento 2: Clean, Flush, Enable, Disable

Magento 2 is a whole universe: it is just as complex and multifaceted and it is just as bloody interesting to figure it out. And although people will hardly ever fully understand the universe, there every chance for us to understand Magento. We can start doing it right now! Today we talk about the cache commands in Magento 2 console.

Magento 2 Cache Clean


Clears only the cache associated with the mage-tags of the specified cache type. Linked files are listed in the var/cache/mage-tags/cacheType content.

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

  1. We get the list of the cache types (the system will check the existence of the specified types, and in case of an error, it will display a warning and stop the script). If the cache type was not specified, then all the available cache types will be processed.
  • config — the configuration of all modules and store settings in the database are combined into one file;
  • layout — stores the layouts of all modules;
  • block_html — html content of the blocks(phtml);
  • collections — stores the queries to the database, default M2 automatically cleans up the collections;
  • reflection — stores the dependencies between the Webapi module and the user module;
  • db_ddl — database schemes;
  • eav — stores the attribute data;
  • customer_notification — stores customer notifications;
  • config_integration — stores the compiled integrations;
  • config_integration_api — stores compiled API integrations;
  • full_page — stores the generated HTML pages;
  • translate — stores the translations from all modules;
  • config_webservice — Web API structure.
  1. The adminhtml_cache_flush_system event is triggered:
  • If the STORES > Configuration > ADVANCED > System > Full Page Cache option is installed and equals Varnish, then the purge request is sent with the X-Magento-Tags-Pattern = .* title.
  • If the STORES > Configuration > General > New Relic Reporting option is enabled, then the data is written to the reporting_system_updates(type = systemCacheFlush, action = json_encode([‘status’ => ‘updated’])) table. If cache clean is performed via the admin panel, then the data is additionally sent to api newrelic.
  • If the STORES > Configuration > ADVANCED > System > Full Page Cache option is installed and equals Built-in-cache, then the adminhtml_cache_refresh_type is triggered (causes a check of point 2.a) and then the clean (TagScope) method is called.
  1. The clean() method is called from the classes, attached to the cache type (the majority of them is located at Magento\Framework\App\Cache\Type\…).
  2. A “collection” of tags of the selected cache types is gathered. There are tags for each cache type. For example, for layout LAYOUT_GENERAL_CACHE_TAG и MAGE (it’s presented in every cache type and is taken from FrontEnd Framework\Cache\Frontend\Decorator\Logger).
  3. Each tag is prefixed by substr(md5(FullPathtoFoldersEtc), 0, 3) . ‘_’; The name of the resulting tag should only contain symbols [a-zA-Z0-9_].
  4. The php – clearstatcache function is executed.
  5. The content of the first cache tag is read, the tag by cache type is specified first (not MAGE) and is read into the array (if the array is empty, the command is completed). Let’s call this array “IDS”.
  6. Go to the IDS array. We get the file id:
    1. The path equals var/cache/mage–‘ . substr($hash(hash(‘adler32’, theNameFromTheArray)), 0, 1) . DIRECTORY_SEPARATOR;
    2. The name = ‘mage–’ . theNameFromTheArray
    3. id = pointA . pointB
  7. The found files are deleted.
  8. Next, the file is blocked (flock) and from the content, just like the IDS array, we collect the IDSNew array. Clear the contents of the file (file cache tag) and save the array_diff in it between IDS and IDSNew.
  9. For cleared cache types, the code is INVALIDATED.

Magento 2 Cache Disable


The script selects the specified types of cache (if such types exist). If we did not specify any cache type, all cache types are selected. After that, the system changes its status to “off”.

Magento 2 Cache Enable


The script selects the specified types of cache (if such types exist). If we did not specify any cache type, all cache types are selected. After the system changes its status to “on”. After that, the cache of the found types is cleared. cache:clean

Magento 2 Cache Flush


Specify the types of cache that you want to delete (separated by space). The specified cache will allow us to consider the backend class, which will be called to clear the cache. If the page_cache is stored in Redis and we call only it, then only the Redis flushDb command will be called.

Before execution, the adminhtml_cache_flush_all event is called (these actions are described in cache:clean). If one type has been specified several times, it will not be called again.

Each type has a backend class with a cache type. For example, you can set caching of the page_cache type via Redis, then the Cm_Cache_Backend_Redis class will be called. In the case of Redis, the Redis flushDb command is called.

If the cache is stored in files, then the Cm_Cache_Backend_File class is called.

The cache folder is used by default, but, let’s say, the Magento-Page-Cache module has a separate page-cache directory, you can also see the full_page cache type binding to FRONTEND_ID page_cache (by default, id = default ’).

Next, the script reads the type-bound folder with the cache, for example: var/cache || var/page_cache. We are looking for (php glob) files in the template. A template may look like this: $dir . $file_name_prefix(by default == mage) . ‘–*’.

All the found files are deleted. Then the directories and the files inside are managed according to this template. After that all the tags are removed from the var/cache/mage-tags or var/page_cache/mage-tags folders.

Magento 2 Cache Status


The list of cache types is read by specifying the links to the modules through the app/etc/di.xml file.

The system requests the information about the status of all types of cache and collects a key => status array. Information is then displayed in the console.

These were the Magento 2 console caching commands, thank you for reading. I will continue the series of articles dedicated to Magento 2 console, there is going to be a large number of useful console commands, so stay tuned.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey


  1. There are many peoples who have confusion regarding Magento cache clean and flush and your post will resolve it.

    Thanks for sharing this informative post.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *