Magento 2 Cache Guide

Mai Xuan Truong

Introduction

Caching is an inseparable part of any system, 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 at 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 the full page cache.
  • var/cache: contains cachable objects except for 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.
Magento 2 Cache Management Grid
Magento 2 Cache Management Grid

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 the command bin/magento cache:clean

    List of cleaned caches
    List of cleaned caches
  • 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
    • Change js code: var/view_processed, var/cache, var/page_cache. You will also have to take browser cache into account since browsers always have their caches for better performance. Sometimes a developer goes nuts on javascript just because he forgot the browser cache, including myself.
    • Add, remove, enable or disable modules: var/di, var/generation, var/cache, var/page_cache.  Alternatively, you can always use the 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. 
[magenest_display_products skus=”alipay-cross-border-payment-gateway”]

Latest Insights

How to Start a Shopify Store in Just Over an Hour Course

Don't want to miss out on our latest insights?
Subscribe to our newsletter.

Disclaimer: By clicking submit, you agree to share your information with us to receive news, announcements, and resources when they are available.