Last updated November 28, 2014. Created on December 22, 2005.
Edited by batigolix, pal4life, jsruzicka, clement.ferrier. Log in to edit this page.

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

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

Other ways of clearing the cache

Drush

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

Drupal 8:

drush cr

Drupal 7 and earlier:

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

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 module Admin_menu 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.

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

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

kopeboy’s picture

If I am not wrong, this is clearing the caches, not rebuilding them.

How about programmatically generating the caches (let's say for a page or a View output)?!

If I have a community page and I want to show "Latest activity", cache it for 1 hour, then clear & regenerate the cache, how should I do?

Why I want to generate it before someone visits the page?
Because there will be so many things on that page to load for the poor man visiting first that he will leave my site.
This is critical for a new portal that has to gain new users..

Thanks!

ehsankhfr’s picture

You can always use another option, CTools cache.

Check ctools_object_cache functions.

I have Finnish style of open-source coding! How about you?