Site Administration

Clear Drupal's Cache for Drupal 8, 9, and 10

Knowing how to clear Drupal's cache is an important skill for any developer. You'll likely find yourself doing it frequently in order to get Drupal to register the changes you make to your code, or other updates you make via the UI. It is also a good first step to trouble shooting problems with your Drupal site: Clear the cache before you do any other debugging to ensure it's not just a bad cache entry.

Goal

Learn three methods of clearing Drupal's cache: via the administrative UI, with Drush, and by truncating tables in the database.

Prerequisites

  • None

Overview

There are two common ways to clear Drupal's cache: via the UI, or with Drush.

Clearing the cache via the UI

Go to the Performance administrative page

In the Manage administrative menu, navigate to Configuration > Performance (admin/config/development/performance).

Select the "clear all caches" button

Image

After successfully clearing the cache Drupal should display the message "Caches cleared."

Add Performance page to Shortcuts

Depending on your role, you may be clearing the cache a lot! Add the Performance page to your Shortcuts so that you can access the page more quickly. Click on the star icon next to the title, "Performance". The star icon will change from an outline of a star to a filled-in yellow star. Access Shortcuts via the Admin Toolbar.

Image

Clearing the cache with Drush

To clear all caches, use the cache-rebuild command: drush cache-rebuild. This will empty all caches and rebuild the data required for Drupal to execute a page request. Alternatively, use the aliased commands drush cr or drush rebuild.

Open a Terminal window and cd to your Drupal site root

If you're using Drush aliases, you can skip this step.

Run the command drush cache-rebuild (drush cr)

Wait for the command to successfully execute and return to your site.

Image

Reload the page you were working on in your browser

It might take a bit longer than usual since the cache has been rebuilt.

What's the difference between cache-clear and cache-rebuild?

You may have been used to using "drush cc all" aka drush clear-cache. Now it's drush cache-rebuild or drush cr, for short. What's the difference and why the change?

Caching is widespread in Drupal and creates many interdependencies. Improperly or incomplete cache flushing operations can cause the site to fatally error. In order to guarantee that old data is properly flushed and the site stays up and running, drush cache-rebuild both rebuilds or re-bootstraps the Drupal site in addition to clearing the cache. Additionally, the Drush command cache is cleared with drush cache-rebuild, since that was one of the caches included in the old drush cache-clear command.

Drush's cache-rebuild command does the following:

  • Clears the APC cache
  • Bootstraps Drupal
  • Calls drupal_rebuild()
  • Clears the Drush cache (to maintain consistency with Drupal 7's drush cache-clear command)

If you'd like to take a peek under the hood of Drush's rebuild method, check out drush/src/Commands/core/CacheCommands.php (line 179).

Truncate cache-related database tables

Another method of clearing the cache is to truncate—or clear all the data from—the cache-related database tables. These are all the tables that begin with "cache_" (after the site-specific table prefix, if there is one).

In a SQL GUI application like Sequel Pro, which is what I like to use, this is easily accomplished by connecting to the server, selecting your site's database, highlighting all of the tables beginning with cache_, ctrl-clicking and selecting Truncate tables. Confirm that you do when the alert pops up. Other database administrative programs will also have the ability to truncate tables.

Image

Or, from the mysql CLI or a SQL command field in a database administrative tool (like PhpMyAdmin or others), you would run the following:

TRUNCATE cache_config;
TRUNCATE cache_container;
TRUNCATE cache_data;
TRUNCATE cache_default;
TRUNCATE cache_discovery;
TRUNCATE cache_dynamic_page_cache;
TRUNCATE cache_entity;
TRUNCATE cache_menu;
TRUNCATE cache_render;
TRUNCATE cache_toolbar;

Recap

In this tutorial, you learned three methods for clearing Drupal's cache: via the administrative performance page at admin/config/development/performance, with drush cache-rebuild or drush cr, and by truncating cache_ tables in the database.

Further your understanding

Additional resources

Drupal Site Administration