Sometimes you want to manually force the cache to be cleared. Perhaps you have a php page which is being cached, and not updating properly. Perhaps the menus have changed or your working on a theme and need a quick way to clear the cache.
You can install a module such as devel, drush or admin_menu, all of which will provide you with a menu item that will clear your caches. However, you may not want a module installed just for this one piece of functionality.

Contents

Drupal 5

Performance Settings Page

To empty the menu cache submit any of the site configuration admin forms (i.e. click on the "Save configuration" button). For example, on the performance settings page (admin/settings/performance) press the "Save configuration" button to clear the menu and page caches.

Clear cache URL or menu link

Create a new page, with the input filter set to a filter that can run PHP. Then use the following snippet, which was shamelessly yanked from devel.module:


/*
 * 2008 Jun 26
 *
 * The code submitted by dharmanerd in his comment probably
 * works better than the original I had, so I've modified this
 * to match his code.
 *
 * The original code for this was shamelessly yanked from
 * `devel.module`; it was the function `devel_cache_clear()`.
 *
 */

// only allow site administrators to visit this page:
if (!user_access('administer site configuration')) {
  drupal_not_found();
}
else {
  drupal_clear_css_cache();
  $tables = array(
    'cache',
    'cache_content',
    'cache_filter',
    'cache_menu',
    'cache_page',
    'cache_views',
  );
  foreach ($tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }
  drupal_set_message('Cache cleared.');
  drupal_goto();
}

Set a URL path for this page (perhaps 'cache_clear') so that you can clear the cache easily. Clear the cache by visiting the URL or add the path to a menu link(watch your permissions).

Code Notes:

  • The drupal_goto at the end of the page will redirect you to either the destination or to the main page of your site; you can change this if you wish.
  • If this code is in a block that was visible on all pages (i.e. including the home page) then the home page would endlessly redirect to itself, breaking your site. The answer is to remove the drupal_goto() from the code, then all caches would be cleared on every page view.
  • Because of the drupal_goto, you can never actually visit this page; you can edit it through the admin/content/node page.
  • You may have to remove the path parameter to the function call drupal_goto, because it gets added to the homepage URL.
  • If you missed some part of the code, which makes the site inaccessible you can remove the code through phpmyadmin, by browsing the block table and editing the appropriate block you created.


Back to top

Drupal 5 and Drupal 6 with Devel

  1. Install DEVEL module: http://drupal.org/project/devel
  2. Enable DEVEL permissions at admin/user/access
  3. Make sure DEVEL block is activated & visible at admin/build/block. Make sure you're editing & using appropriate theme block.
  4. Click on 'Empty cache' link located inside the DEVEL block. Or use the following direct link: http://www.example.com/devel/cache/clear

by, Onopoc

Drupal 5 and Drupal 6 with Drush

http://drupal.org/project/drush
Once drush is configured, switch to the root of your Drupal directory (e.g. cd /path/to/drupal) and execute this command: drush cache clear. Works with all Drupal version, AFAIK.
by, greg.harvey


Back to top

Drupal 6

Performance Settings Page

Clearing the cache can done from the performance settings page (admin/settings/performance) use the "Clear cached data" button (to clear all caches).

Drupal 6 clear cache URL or menu link

Tired of opening (admin/settings/performance) all the time, scrolling down and pressing the Clear cached data button?
Make a menu link for an easy one click cache clear without installing admin_menu or devel modules.

Create a page using the PHP Input format with the following content (taken from the devel module):

  drupal_flush_all_caches();
  drupal_set_message('cache flushed.');

Create a menu item in the Navigation menu with the path admin/clearcache (/admin/* is restricted on my site).

Now your one click away from clearing the cache.
Don't want a menu link? Visit the URL of the page to clear the cache.

by, tian

Comments

mattez’s picture

Hi, I tried that same, it works, but it looks that script is running every time I do many different thing, like create new content!, or when I change FCK editor setup and other changes...

I created a page using the PHP Input format with the content written higher in this page + redirect back to page from where it was called.

  drupal_flush_all_caches();
  drupal_set_message('My message');
  drupal_goto($_COOKIE['referer2']);

Shouldn't be there some "if", like page is in "view state" - I'm non-programmer, sorry...
Somehow Drupal visit that page,and run the script, without need to click right link.
So i tried this (no referrer set up), I know it is definitely ugly solution, BUT IT WORKS FOR ME:

if($_COOKIE['referer2']) {
  drupal_flush_all_caches();
  drupal_set_message('My message');
  drupal_goto($_COOKIE['referer2']);
}

How to make this like real DRUPAL solution? Thanx for our tips!

stefgosselin’s picture

The function used to clear the cache, drupal_flush_all_caches() is used in this solution. That same function is called from a few other places in core and is also called by various 3rdparty modules.

In short, Drupal does not mysteriously hit the page you created with drupal_flush_all_caches() : this function can be called elsewhere ( and it is ) with the same results .

dr3d’s picture

  drupal_flush_all_caches();
  drupal_set_message('Caches flushed.');
  drupal_goto(referer_uri());

is more reliable for me

EDDYL’s picture

I had problems with Gmap markers that went invisible after been seen once.
Even if cache was disabled on the site, I had to clear cache to get the markers back.
I just added drupal_flush_all_caches(); in my view and all is back in order.
This is quite unacceptable bug to have cache running even if you dis-activate it in admin settings !
Edouard

CodigoVision’s picture

I have a memory problem on a site we're developing and the admin menu was causing the white screen of death, so this is the perfect solution. Thanks!

crimsondryad’s picture

Site configuration allows you to do a lot of stuff....including change "Salt" values if you have that module installed, change allowable file extensions if you have imagecache / imageapi installed, etc. We have vendors who are developing on our staging environment and basically we don't want to let them run amok with changing settings they really don't need to have.

One of the most recent requests was the ability to clear the cache / rebuild the theme registry. This is on our staging environment, not a live site. I need to give them access to clear the cache *without* giving them administer site config perms. Anyone have any ideas on how to do this?

It's one of the things I both love / hate about Drupal..to me it seems like the perms are too granular in some areas (like having to specifically enable every single CCK field), but not specific enough for things like administration. I would love to have the ability to check one button and automatically enable all the fields a developer creates. And I'd like to be able to give them more specific admin type perms that aren't as security critical.

hruodland’s picture

The Drupal 6 page doesn't seem to work for Drupal 7. Any suggestions?

MadMakz’s picture

testertesters’s picture

I implemented a page for clearing caches in my custom module (called "utility") like this. The page to clear caches is /clear. It is only accessible to people with "administer site configuration" rights

function utility_menu() {
	$items = array();
	$items['clear'] = array(
		'title' => 'Clear cache',
		'page callback' => 'utility_clear_cache',
		'access arguments' => array('administer site configuration')
		);
	return $items;
}

function utility_clear_cache() {
	drupal_flush_all_caches();
	drupal_set_message("Caches cleared");
	drupal_goto(referer_uri());
}

doublejosh’s picture

Any way to clear the page cache of just one page/node/URL?
With a large site it's not practical to go clearing all cache willy-nilly, but it's certainly needed to clear the page cache for an individual page.
Boost has this via an admin block, but is there a tool like this for core cache?

nguyentran’s picture

This function take a long time to run, are there any function to clear cache of individual page or some thing like that.

crimsondryad’s picture

As I mentioned before, having to give Site Configuration perms isn't great. As our sites scale in size though, there's another issue: we don't want to clear the entire site cache. We would really like the ability to clear some segments of the cache ( like in drush ). For example, just the theme registry or the views cache.

Currently we're using a sledgehammer to swat a fly. Optimally, this would be a configuration option in core that would allow a site owner to choose what type of cache clearing they would like to allow by role.

kikecastillo’s picture

Hi,

I just found how to create a file in Drupal 7:

// 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();

Anyone knows how to do in Drupal 8?

Thanks a lot !