+8801306001200
 |   | 
Magento 2



Comprehensive Guide to Clearing Magento 2 Cache

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. This is a crucial skill for both store owners and developers. The following guide provides a comprehensive overview of how to manage and clear your Magento 2 cache effectively.

1. Using the Admin Panel

Magento provides a user-friendly option to clear the cache directly from the Admin Panel. This is the recommended method for non-developers or for minor updates to your store.

  1. Login to Magento Admin: Go to your store’s /admin URL and log in.
  2. Navigate to Cache Management: From the left sidebar, go to System > Tools > Cache Management.
  3. Select Cache Types to Clear:
    • On the Cache Management page, you’ll see a list of cache types such as Configuration, Layouts, and Blocks HTML output.
    • You can either select individual cache types to refresh or use the “Select All” checkbox.
  4. Flush Cache Storage:
    • Once you’ve selected the caches, choose Refresh from the dropdown menu and click “Submit” to clear the selected caches.
    • Alternatively, you can click the “Flush Cache Storage” button to clear all cached data, including data stored in external caches like Redis or Varnish. This is a more comprehensive action.

2. Using the Command Line Interface (CLI)

For a more efficient and thorough cache clearing, especially when dealing with a large Magento store or during development, using the command line is highly recommended. This method is often faster and more powerful. For more information on using the CLI, refer to the official Magento DevDocs.

  1. Login to Your Server: Connect to your server via SSH 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):
    cd /path/to/your/magento
  3. Clear Cache Using Magento CLI:
    • To clear all cache types, use the following command. This command marks the cache as outdated so it will be regenerated on the next request.
      php bin/magento cache:clean
    • To completely remove all cached data from the filesystem, use this command:
      php bin/magento cache:flush
  4. Clear Specific Cache Type: If you want to clear a specific cache type (e.g., configuration, layout), use:
    php bin/magento cache:clean <cache_type>

    Example for clearing the configuration cache:

    php bin/magento cache:clean config

    Some common cache types you might want to clear include:

    • config (Configuration)
    • layout (Layouts)
    • block_html (Blocks HTML output)
    • full_page (Full Page Cache)
    • collections (Collections Data)

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 for troubleshooting when other methods fail.

  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: Use the following command to clear the cache. Be cautious, as this will completely remove all cached files.
    rm -rf /path/to/magento/var/cache/*
    rm -rf /path/to/magento/var/page_cache/*
    rm -rf /path/to/magento/var/generation/*

4. Clearing External Caches (Varnish, Redis)

Magento 2 often works in conjunction with powerful external caching solutions like Varnish or Redis. If you’re using these, you need to clear their caches as well.

Varnish Cache:

  1. Via Command Line: If you’re using Varnish, you can clear its cache by flushing it with the command:
    varnishadm "ban req.url ~ /"
  2. Via Magento Admin: If your Varnish is properly integrated, clicking “Flush Cache Storage” in the Magento Admin Panel should also clear the Varnish cache.

Redis Cache:

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

redis-cli flushall

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. For a deep dive into cache types and management, it is always recommended to consult the official Magento documentation.