Clearing the cache in Magento 2 is an essential task to ensure that changes you make to your store (like product updates, theme changes, or configuration modifications) are applied correctly. Magento uses various types of cache, and clearing these caches will help in reflecting the changes immediately.

Here’s a comprehensive guide on how to clear Magento 2 cache:

1. Using the Admin Panel

Magento provides an option to clear the cache directly from the Admin Panel. Here’s how to do it:

  1. Login to Magento Admin: Go to the Magento Admin Panel by accessing your store’s /admin URL.
  2. Navigate to Cache Management: From the left sidebar, go to System > Tools > Cache Management.
  3. Select Cache Types to Clear:
    • In the Cache Management page, you’ll see a list of cache types such as Configuration, Layouts, Blocks HTML output, etc.
    • You can either select individual cache types or use the “Select All” checkbox to clear all caches.
  4. Flush Cache Storage:
    • Once you’ve selected the caches, click Flush Magento Cache to clear the selected caches.
    • Alternatively, you can click Flush Cache Storage to clear all cached data, including data stored in external caches like Redis or Varnish (if you’re using them).


2. Using Command Line Interface (CLI)

For a more efficient and thorough cache clearing, especially when dealing with a large Magento store, using the command line is recommended.

  1. Login to Your Server: SSH into your server where Magento is installed.
  2. Navigate to the Magento Root Directory: Use the command below to change to the Magento root directory (where the bin/magento file is located):
    bash
    cd /path/to/your/magento
  3. Clear Cache Using Magento CLI:
    • To clear all cache types, use the following command:
      bash
      php bin/magento cache:clean

      This command clears the cache, but it doesn’t remove the cached files from the filesystem. It only marks them as outdated, so Magento will regenerate them the next time they are required.

    • To remove cache types completely (not just clean them), use:
      bash
      php bin/magento cache:flush

      This clears and removes all cached data from the filesystem.

    • To clean and flush all types of caches, you can use:
      bash
      php bin/magento cache:clean && php bin/magento cache:flush
  4. Clear Specific Cache Type: If you want to clear a specific cache type (e.g., configuration, layout), use:
    bash
    php bin/magento cache:clean <cache_type>

    Example for clearing the configuration cache:

    bash
    php bin/magento cache:clean config

    Some common cache types include:

    • config
    • layout
    • block_html
    • collections
    • reflection
    • db_ddl
    • compiled_config
    • eav
    • customer_notification



3. Clear Cache via File System

If you are experiencing cache-related issues and want to completely remove Magento’s cache manually, you can do so by deleting the contents of the cache directories. This method is particularly useful when dealing with issues like corrupted cache files.

  1. Navigate to the Magento Cache Directories: Magento stores cache data in the following directories:
    • /var/cache/
    • /var/page_cache/
    • /var/generation/
  2. Delete Cache Files: You can manually delete the files in these directories, but avoid deleting the directories themselves. You can use the following command to clear the cache:
    bash
    rm -rf /path/to/magento/var/cache/*
    rm -rf /path/to/magento/var/page_cache/*
    rm -rf /path/to/magento/var/generation/*

    Be cautious with this method because clearing caches manually might cause temporary performance issues until the caches are rebuilt.

4. Clear Cache from Browser

After clearing cache from Magento’s admin or CLI, you should also clear your browser’s cache to ensure that outdated files aren’t being served.

  • Clear your browser cache or open the site in an incognito window.
  • Alternatively, you can use the browser’s developer tools to disable cache temporarily.

5. Clear Full Page Cache (FPC)

Magento 2 comes with Full Page Caching, which speeds up page loading by caching entire pages. If you’re using Varnish or another FPC, you need to clear it separately.

Varnish Cache:

  1. Varnish Cache via Command Line:
    • If you’re using Varnish, you can clear its cache by flushing it via the following command:
      bash
      varnishadm "ban req.url ~ /"
  2. Varnish Cache via Magento Admin:
    • Go to System > Cache Management and flush the Varnish cache if integrated.

Redis Cache:

If you’re using Redis for session storage or caching, you can flush it using the following commands:

bash
redis-cli flushall

6. Disable/Enable Cache in Magento

Magento allows you to disable cache types during development to speed up changes without needing to clear the cache all the time.

  1. Navigate to Admin Panel: System > Tools > Cache Management.
  2. Disable Cache Types: Select the cache types you wish to disable and click Disable. This prevents Magento from using those cache types.
  3. Enable Cache Types: When you’re done, you can enable the cache again.

Alternatively, use the CLI to disable cache types:

bash
php bin/magento cache:disable <cache_type>

For example, to disable the configuration cache:

bash
php bin/magento cache:disable config

7. Clear Cache after Deployment

If you’ve made significant updates to your store (e.g., theme updates, module installation), it’s important to clear caches after deployment to ensure everything functions correctly:

  1. Run the following commands after deploying updates or changing settings:
    bash
    php bin/magento setup:upgrade
    php bin/magento setup:di:compile
    php bin/magento cache:flush
  2. These commands will apply configuration changes and ensure that the caches are refreshed with the latest data.

Conclusion

Clearing Magento 2’s cache can be done in multiple ways: from the Admin Panel, using the Command Line Interface (CLI), or manually via the file system. The choice of method depends on the situation, such as development, production, or troubleshooting. Regularly clearing caches is an important task to ensure your store is up-to-date and functioning correctly, especially when making changes or troubleshooting issues.