Support – Knowledge Base

How to flush your caches

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.

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.

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.