How to clear Drupal’s cache


To totally unlock this section you need to Log-in


Login
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 caches when moving a site from one host to another.

Clearing the caches can also useful when installing new modules or themes, and as a first step in troubleshooting. Clearing caches is 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.

Clearing the cache

  • The easiest way to clear the Drupal cache is to go to Administration > Configuration > Development > Performance;
  • Click the button "Clear all caches";

Other ways of clearing the cache

Drush

Run drush cc all.

To clear the cache, even if Drupal is broken, you can run the following command:

drush sql-query "DELETE FROM cache"

sql-query executes sql queries in the database where Drupal is installed.

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_.

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.

<?php

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');
?>

How to clear main cache tables through mySQL query

We can delete the Drupal cache through mysql also. For that you need to use following mysql commands in phpmyAdmin or through mysql command line.

The main cache tables are the following:

DELETE FROM cache;

DELETE FROM cache_filters;
DELETE FROM cache_menu;
DELETE FROM cache_page;
DELETE FROM watchdog;

NOTE: the watchdog module monitors your system and records system events in a log (in the watchdog table) you can review. This helps you get a quick overview of activity on your site. Since the log records events in sequence, it can also be useful for debugging site errors. It can grows over 300MB periodically if Drupal is configured to log all info.

Note that starting with Drupal 6.x, Watchdog has been replaced by the dblog and syslog modules. Dblog is similar to watchdog; Syslog allows Drupal's logging to be integrated with the server's syslog facility.

Clear cache from phpMyAdmin

To clear the Drupal's cache from phpMyadmin select all tabels those begin with cache as image below:

How to clear Drupal's cache

How to clear Drupal's cache

After you've sent the deletion command you will be directed to a confirmation page with sql statements like this:

Do you really want to :


TRUNCATE `cache`;
TRUNCATE `cache_apachesolr`;
TRUNCATE `cache_block`;
TRUNCATE `cache_content`;
TRUNCATE `cache_filter`;
TRUNCATE `cache_form`;
TRUNCATE `cache_menu`;
TRUNCATE `cache_page`;
TRUNCATE `cache_path`;
TRUNCATE `cache_rules`;
TRUNCATE `cache_update`;
TRUNCATE `cache_views`;
TRUNCATE `cache_views_data`;

Confirm clicking OK and the DB cache will be cleared.