Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

Clearing or rebuilding Drupal's cache

Last updated on
December 1, 2016 - 12:04

To create a page Drupal needs to make several database queries. This can slow down websites with a lot of traffic. To make websites faster Drupal stores web pages in a cache.

It is a good practice to clear or rebuild caches when moving a site from one host to another. Clearing the caches can also be useful when installing new modules or themes, and as a first step in troubleshooting. During development it is necessary to clear the cache very frequently.

Clearing caches are relatively harmless. Sites might slow down for a bit afterwards while the cache fills back up.

Note that there are sometimes caches other than the Drupal cache that can affect website performance. For example, some hosting services use a cache called "Varnish" to improve performance. Also, individual web browsers maintain their own caches on a user's computer or device.

The procedure below is for clearing Drupal's internal cache. To clear a non-Drupal cache, please refer to the procedures for those caches.

About clearing the theme cache.

Clearing the cache

  1. The easiest way to clear the Drupal cache is to go to Administration > Configuration > Development > Performance (http://example.com/admin/config/development/performance)
  2. Click the button "Clear all caches"

Other ways of clearing the cache

Drush

The most convenient way to rebuild Drupal's cache is by using Drush.

Drupal 8:

drush cache-rebuild

Drupal 7 and earlier:
drush cache-clear all or alternately,

drush cc all

Run update.php

Running update.php (http://example.com/update.php) is another way of clearing the cache. If you cannot login or have no user 1 rights you will need to set $update_free_access = TRUE; first in /sites/default/settings.php. Do not forget to set this back to FALSE afterwards.

A PHP snippet

Use the following code to clear specific cache type from within a module. If you want to clear all the caches similar to the functionality of the button on the "Performance" page, call this snippet for all tables beginning with the word "cache".

<?php
db_query("DELETE FROM {cache};");
?>

In the database

Use phpMyAdmin or another tool to access the database tables and TRUNCATE (empty, not remove) all tables that start with cache_.

In phpMyAdmin, if you like to clear the whole cache_* table, select it and choose "Truncate" from the "with selected" drop-down menu at the bottom of the page. For the command line, see here:
http://dev.mysql.com/doc/refman/5.1/en/truncate-table.html

Drupal 8 - MySQL Snippet :

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;

Devel module

The module Devel makes it easy to clear Drupal's cache. Just install this module and enable the "Developer/Development Block" for easy access to cache clearing. Note that "Devel" is a module designed to make development easier and should generally not be enabled on a production site.

Admin_menu module

The Administration menu (admin_menu) module has several cache clearing options for menu, page requisites, theme registry, cache tables and administration menu. It also has cron and update.php links too.

A PHP file to clear caches and rebuild the routing tables

Be careful not to leave this on the server as anyone can clear caches if they know the file name. Create a file named clear.php with the code below. Place the file in the Drupal base directory and run it by browsing to http://example.com/clear.php.

Drupal 6

<?php
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
drupal_flush_all_caches();
?>

Drupal 7 version

<?php
// Define static var.
define('DRUPAL_ROOT', getcwd());
// Include bootstrap.
include_once('./includes/bootstrap.inc');
// Initialize stuff.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Clear cache.
drupal_flush_all_caches();
?>

Clearing specific entries in cache tables<>

The Drupal cache API can be used to clear specific items. Caching back ends can be switched (to memcache, to flat files, etc.) without having to rewrite any code.

cache_clear_all('content:' . $MYNID, 'cache_content', TRUE);

The example above clears all items from cache that start with 'content:$MYNID*'. To only remove one specific row, drop the $wildcard parameter (the "TRUE" statement in the function call) and change the format of the first parameter to omit the asterisk, which functions as a wildcard.

<?php
cache_clear_all('content:' . $MYNID . ':' . $MYNID, 'cache_content');
?>