20

How do I empty the Drupal caches:

  • without the Devel module
  • without running some PHP Statement in a new node etc.
  • without going into the database itself

Effectively, how do you instruct an end user to clear his caches?

iconoclast
  • 21,213
  • 15
  • 102
  • 138
alexanderpas
  • 2,712
  • 2
  • 23
  • 33
  • And why can't you use your second solution? – Sam Aug 04 '09 at 22:48
  • 4
    Because certain links are still pointing to their development domain. Instructing a luser to enter PHP? that has to go wrong, and usually in a ultimate way! – alexanderpas Aug 04 '09 at 23:00
  • 1
    @eyurdakul Sam's comment is quite constructive, but yours was not. Please don't leave comments telling people you're flagging their posts/comments, *just flag them*. – user229044 Dec 07 '16 at 23:02
  • @eyurdakul - it was a legitimate question for me at the time. Of course the 'luser' wouldn't be entering their own PHP, but running something the OP had coded. I don't think you have to let people know that you flag them and also insult them at the same time. – Sam Dec 07 '16 at 23:02

12 Answers12

44

When you are logged as an admin (obviously, not every user of the site has to power to clear the cache), there should be a page in "Administer > Site Configuration > Performance".

And, at the bottom of the page, there should be a button (something like "Clear cached data") to clear the cache

As far as I remember, there's no need for Devel to do that, and you really don't need to go to the database, nor run some home-made PHP code.


As a reference, you can take a look at How to Clear Drupal Server-side cache

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • thanks, that was exactly the answer that i was looking for. as a seasoned drupaller I'm so used to having access to the devel module, i tend to forget the most easiest actions ;) – alexanderpas Aug 04 '09 at 22:57
  • You're welcome :-) (I admit, I had to think for a while to remember -- to much using of Devel too ^^ -- And I prefered checking with google before posting, as a security measure :-D ) – Pascal MARTIN Aug 04 '09 at 23:03
  • Link is now broken, pinging you so that you can fix it – Mr. Alien Jan 22 '15 at 07:40
  • It's too bad there isn't a direct URL to trigger it (instead of a button click). I was working on the main menu template, and I did something stupid, so now every page crashes... including performance. I can't click the button. – Mir May 31 '21 at 22:51
20

You can also use the Drush module, which allows you to use the command line to execute popular Drupal commands, like "drush cron" or "drush cache clear".

Niels
  • 9,264
  • 4
  • 29
  • 26
8

If you want to clear the cache from a module, you can use the following code.

drupal_flush_all_caches();
sagesolutions
  • 491
  • 4
  • 9
4

I have the easiest solution for that. Install admin_menu module (actually not only for this purpose, once you have installed this module, you wont regret it for sure, link: http://drupal.org/project/admin_menu). Ok, then on a newly appeared top dropdown menu hover your favicon and dropdown menu will appear, and you will see: Flush all caches menu. One click - one flush. Moreover you can flush all caches together or select what to flush: Pages, menu, themes etc. Try and you will never go back )

user143520
  • 491
  • 1
  • 4
  • 5
  • Admin menu is bloated and can create performance issues for people logged in. It also had issues back in the day where it would call menu_router_build on almost every page. – Kevin Mar 28 '10 at 17:54
3

It would be awesome if you could just GET the behavior by hitting:
http://drupal.local./admin/settings/performance?op=Clear%20cached%20data
but you can't.
However I do want to note the URL for short-cutting through the admin menu (use the latter part):
http://drupal.local. /admin/settings/performance

dlamblin
  • 43,965
  • 20
  • 101
  • 140
1

I found the following at: http://www.drupalgardens.com/content/clear-all-caches-not-working

There's another layer of caching around the site which "clear all caches" does not affect, you're right. That's the layer that stores the content for anonymous users.

If you want to bypass the cache for testing purposes, you can add a junk query string to the end of your site path. For example, if you wanted to bypass the cache on example.drupalgardens.com/foo you could visit example.drupalgardens.com/foo?bar=baz or any other random text set up like ?xxxxx=xxxxx.

This helped me, because I have had issues where clearing the cache under Configuration > Performance didn't seem to help.

ansorensen
  • 1,276
  • 1
  • 14
  • 29
  • This is useful (so I just upvoted it back up to zero) but it doesn't really answer the question, which is probably why it was downvoted. You may want to remove it before someone else downvotes. – iconoclast Nov 06 '13 at 17:55
1

The above code is for Drupal 6.

For Drupal 7 the flush-cache module would be as follows:

<?php 
/**
 * Implementation of hook_menu()
 */
function flush_cache_menu() {
  $items = array();

  $items['flush-cache'] = array(
  'type' => MENU_NORMAL_ITEM,
  'title' => t('Flush the cache'),
  'description' => 'Flush all website caches to make sure it updates to relect '.
    'your recent changes.',
  'page callback' => 'flush_cache_custom_callback',
  'access callback' => user_access('flush cache'),
  );

  return $items;
}

/**
 * Implementation of hook_permission()
 */
function flush_cache_permission() {
  return array(
    'administer my module' => array(
      'title' => t('flush cache module'),
      'description' => t('Content admin flush cache.'),
    ),
  );
}

/**
 * Function that flushes the cache
 */
function flush_cache_custom_callback() {
  drupal_flush_all_caches();
  return 'Caches were flushed.';
}

Note: that you then flush it by going to:

sitename.com/flush-cache

Make sure you give them permission on the permission page. Clear cache once the "normal" way if the permission doesn't appear after turning the module on.

This is preferable when you don't want your client to get access to the admin menu but you still want them to be able to flush the cache.

1

On-demand clearing can be done in Administer > Site Configuration > Performance.

You should setup the cron job to run every hour (or whatever interval to your liking).

When cron is run on Drupal, all caches are cleared and rebuilt without the need for a human to manually do it.

If this question pertains to theming, you should disable the caching mechanisms (css/js aggregation) and you won't have to clear the cache data when you make changes.

Kevin
  • 13,153
  • 11
  • 60
  • 87
1

HERE YOU GO:

I had to de-install the "devel" module (it was incompatible with Special Menu Items, which I needed worse), so I made my own.

Anywhere you see MODULENAME replace it with the name of your module.

STEP 1: Add to any module (preferably one of your custom modules) in the HOOK_MENU, before the "return $items" line:

// short cut for flushing the caches:
$items['flush-cache'] = array(
  'type' => MENU_CALLBACK,
  'title' => t('Flush the cache'),
  'description' => 'MODULENAME Custom Cache Flush',
  'page callback' => 'MODULENAME_flush_cache',
  'access callback' => TRUE,
);

STEP 2: Now, in the same module file, where it's not "inside" any other function, add:

/**  Page callback  **/
function MODULENAME_flush_cache() {
  drupal_flush_all_caches();
  return 'Caches were flushed.';
}

Now, you can just go to the URL "/flush-cache" on your site to flush the cache. (After you flush the cache one last time the old way.)

STEP 3: If you want it REALLY convenient, add the following to your page.tpl.php file. You can put it pretty much anywhere between <body> and </body>. NOTE: $my_is_test is a variable I use that's TRUE on my test system, and FALSE in production. If you don't have something like that, replace it with TRUE or FALSE to turn it on or off:

<?php if ($my_is_test): ?>
<a style="text-align:left; position:absolute; right:2px; top:20px;" href="<?=$base_path?>flush-cache" onclick="this.innerHTML = '<b><blink><big>Wait...</big></blink></b>';">flush</a>
<? endif; ?>

And voila! You have a "flush" link at the top-right corner of every page you can click on. Feel free to change the "right" and "top" amounts (or change "right" to "left" or "top" to "bottom" to put it wherever you like it. This link positioning only works on modern browsers, but it's only for you, so it shouldn't be a problem, right?

Jeffiekins
  • 569
  • 6
  • 14
0

In Drupal 8, the admin menu module isn't quite ready for use yet. And it will probably get replaced with Drupal "Toolbar". So right now there's no easy way to clear cache, without actually going to:

admin/config/development/performance

The only alternative is to add a menu item in the existing toolbar. This can be done by using this module, but as you can see, it still needs a bit of work. I got it working, but had to make a few tweaks.

rockstardev
  • 13,479
  • 39
  • 164
  • 296
0

The following module creates a menu item that is accessible only to users with the permission "flush cache", which this module makes available on the regular user permissions page.

/**
 * Implementation of hook_menu()
 */
function flush_cache_menu() {
  $items = array();

  $items['flush-cache'] = array(
  'type' => MENU_NORMAL_ITEM,
  'title' => t('Flush the cache'),
  'description' => 'Flush all website caches to make sure it updates to relect '.
    'your recent changes.',
  'page callback' => 'flush_cache_custom_callback',
  'access callback' => user_access('flush cache'),
  );

  return $items;
}

/**
 * Implementation of hook_perm()
 */
function flush_cache_perm() {
  return array('flush cache');
}

/**
 * Function that flushes the cache
 */
function flush_cache_custom_callback() {
  drupal_flush_all_caches();
  return 'Caches were flushed.';
}
Shaun Dychko
  • 825
  • 1
  • 9
  • 11
-1

use drush and this command: drush cc all

If you're using Boost to cache you need to be more specific:

drush @alias_name cc all
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
  • You can not instruct an end user to clear the cachenwitha command line since they usually just got access to the user interface (back office). – Laurent Fauvel Jan 10 '16 at 07:53