How to flush your caches

in Caching

When making adjustments to Magento’s configuration, e.g. installing or updating extensions or the Magento installation itself, it is often required to flush the caches.
As Magento is a resource hungry beast, there are several caches in use to temporarily store data to speed up the webshop.

To make life easier, this article contains a collection of instructions on how several caches can be flushed.

Although most of these commands can be done from the Magento backend, if you run into caching issues, the backend is most often the first to suffer from inaccessibility, making it impossible to flush any cache from the backend.

To ensure you are always able to flush the caches, this article describes the method to do this on the command line. This way you can resolve caching issues whether the backend is accessible or not.

View the status of the caches

Using n98-magerun or n98-magerun2, you can very easily retrieve a listing of the status of the cache.
To do this, log in on your Hypernode and enter the public content directory. For Magento 1 this is /data/web/public and for Magento 2 this is /data/web/magento2.

Then issue the command:

On Magento 1:

n98-magerun cache:list

On Magento 2:

n98-magerun2 cache:list

This will show you an overview similar to this one:

  Magento Cache Types

+------------------------+--------------------------------+---------+
| Name                   | Type                           | Enabled |
+------------------------+--------------------------------+---------+
| config                 | Configuration                  | 1       |
| layout                 | Layouts                        | 1       |
| block_html             | Blocks HTML output             | 1       |
| collections            | Collections Data               | 1       |
| reflection             | Reflection Data                | 1       |
| db_ddl                 | Database DDL operations        | 1       |
| eav                    | EAV types and attributes       | 1       |
| customer_notification  | Customer Notification          | 1       |
| full_page              | Page Cache                     | 1       |
| config_integration     | Integrations Configuration     | 1       |
| config_integration_api | Integrations API Configuration | 1       |
| translate              | Translations                   | 1       |
| config_webservice      | Web Services Configuration     | 1       |
+------------------------+--------------------------------+---------+

Which will show each available cache for the shop and it’s status.

Flush the Magento caches

We recommend all Hypernode users to use Redis for caching.
As the file cache on cloud nodes is often shared with other nodes on the same hardware, writing lots of files can vary a bit in speed and duration.

The memory for the node however, which Redis uses for storing key/value pairs, is reserved for a single Hypernode and is never shared with other servers running on the same hardware. This makes using Redis much faster than using the file caching mechanism that is enabled by default.

Flush a single cache

Magento stores several cache types in one single Redis database. To selectively flush a single cache type rather then all caches, you can use the utilities provided by n98-magerun and n98-magerun2.

For example if we only want to flush the full page cache:

On Magento 1:

n98-magerun cache:clean full_page

On Magento 2:

n98-magerun2 cache:clean full_page

Flush all caches at once

If you want to flush all Redis caches at once, you can easily flush all Redis databases using the flushall command:

redis-cli flushall

This will flush out all available Redis databases.

NB: If the sessions are stored in Redis too, don’t flush out all databases, but selectively use the ones that are used for caching. Read the next section to learn how.

Flush all caches when storing the sessions in Redis

If you use Redis for the sessions too, flushing all Redis databases at once will flush the sessions database as well, forcing all your visitors to re-login again.

To flush only the databases that are used for caching and not for sessions, use the flushdb command for Redis rather then using flushall.
IE: If database 0 and 1 are used for caching and database 2 is used for sessions, only flush database 0 and 1:

redis-cli -n 0 flushdb
redis-cli -n 1 flushdb

Or using a for loop:

for i in 1 2; do
  redis-cli -n "${i}" flushdb
done

Flush the PHP OPcache

Flushing the OPcache can be done from the Magento backend, but if you run into issues regarding the upgrade of Magento itself or it’s extensions,
the backend is most often the first to suffer from inaccessibility.

Therefor to flush the OPcache in case of errors, you need to be able to flush the OPcache from the command line.
This can be done using the opcache_reset(); PHP function.

If you call this function using the command line, PHP will try to reset the OPcache for the php-cli which won’t work for PHP-FPM.
Therefor if you want to flush the OPcache, we should do this by creating a PHP script which we request using curl or a web browser.

To do this, first create a flush script:

mkdir /data/web/public/private
echo '<?php header("Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate"); opcache_reset(); echo "Opcache Flushed"; ?>' > /data/web/public/private/flush_opcache.php

Second, create some Nginx config to ensure you only flush the OPcache and not the rest of the interweb:

location /private/ {
    allow 1.2.3.4; ## Replace with your own ip address
    allow 2.3.4.5; ## Replace with the ip address of the hypernode itself
    deny all;

    location ~ \.php$ {
        echo_exec @phpfpm;
    }
}

Save the snippet as /data/web/nginx/server.opcache.
Now you can flush the opcache by executing:

curl http://appname.hypernode.io/private/flush_opcache.php

… Or by visiting the URL in your browser.

Flush the Varnish cache

Flushing the Varnish cache can be done using the Magento backend panel, but in case of problems, when the backend is not accessible,
you might want to be able to flush the cache from the command line to fix any Varnish related caching issues.

You can flush the cache for a single domain, a specific URL or just the cache as a whole.

  • Flush a single domain:
    varnishadm "ban req.http.host ~ www.mydomain.com"
  • Flush a specific file type
    varnishadm "ban req.url ~ .css"
  • Flush a specific file type of a single domain:
    varnishadm "ban req.http.host ~ www.mydomain.com && req.url ~ .css"
  • Flush a specific URL
    varnishadm "ban req.url ~ /admin/something"
  • Flush the cache as a whole
    varnishadm "ban req.url ~ ."

Flush your wsdl cache

The wsdl cache is used for API soap calls to magento. In some very rare cases this cache can get corrupted, causing errors on all API calls to magento.

In most of the cases this causes on of the following errors:

  • Resource path not callable
  • Invalid API path
  • Is not a valid method

This can be resolved in some cases by removing the WSDL cache file so this file is refetched with the next api call.
To clear the WSDL cache, remove the cache files in /tmp:

rm /tmp/wsdl-*

NB: This does not solve the following error: SOAP error: Parsing WSDL: Couldn’t load from "": failed to load external entity. This error is related to Magento not being able to access it’s own API

1