Caching is an inseparable part in any systems, especially for a complex and data-dependent one like Magento 2. Almost everything in Magento has its own cache: modules, themes, etc.. Meanwhile, improper cache management may turn your developing experience into a living hell. So we will have a quick look on Magento 2 cache directories and how to clear them.
Types of Magento 2 Cache
Generally almost all Magento caches are stored in var/ directory. Specifically:
- var/page_cache: generated from full page cache
- var/cache: contains cachable objects except the page cache
- var/composer_home: Setup Wizard home directory. In most cases, you shouldn’t touch this directory.
- var/generation: contains generated code by factory classes, proxy generated objects, and interceptor classes .
- var/di: contains compiled dependency injection configuration for all modules
- var/view_preprocessed: contains minified templates and compiled styling files
Cleaning the caches
First of all, as a developer, it is recommended that you turn off all caching by command bin/magento cache:disable
In addition, for admins, there’s a caching interface in System -> Cache Management where you can clear the caches within a click of a button, but as a developer, there are a few tricks that you can use:
- Clear the cache with command bin/magento cache:clean
- Depending on what type of code you modified, there are different ways that you can clear the cache. Hence, just go for rm -rf <cache directory>
- Change a block or controller’s construct method, or any class that has a plugin related to it: var/di, var/generation
- Add, remove, enable or disable module: var/di, var/generation, var/cache, var/page_cache. Alternatively, you can always use upgrading command bin/magento setup:upgrade for this case.
That’s all to caching for now. Effectively manage this and it will save you from a sky of trouble as a result.