22

On development platform caching can be a pain. Can I turn it off? Will it affect any of the modules that require it?

10 Answers 10

19

Install the devel module which adds a cache clear option for administrators. You'll find a devel block to add to a region for quick cache clearing, or if you install the admin menu and get a quick menu in the top left for clearing various caches in Drupal.

If you like the command line install drush and use the drush cc all command to clear your sites caches. Beware that currently drush cc doesn't clear the Varnish cache if that's applicable to your setup.

9

If you are using Drupal 7 you can always set your caching to using DrupalFakeCache, which is normally only used during the installation process. To set that, drop the following code snippet in your settings.php:

include_once(DRUPAL_ROOT . '/includes/cache.inc');
include_once(DRUPAL_ROOT . '/includes/cache-install.inc');
$conf['cache_default_class'] = 'DrupalFakeCache';
4

'Performance' page in administration > site configuration will allow you to enable / disable caching of selected site elements. Theme templates are typically cached more persistently but can be flushed within the same 'performance' page.

'Administration menu' module will provide a shortcut in a toolbar if cache flushes need to be done more regularly - during theme dev I find myself constantly flushing.

4

As far as I know there aren't any modules that require caching. If you don't want to turn it off you can clear it in the Site Settings > Performance section.

Even with caching disabled Drupal won't recognise new files added to themes until you clear the cache.

There's a some information on different ways to clear the cache over at Drupal.org

4

In addition to the methods Steve H mentioned, using the Devel module opens up a few more options, including:

  • Rebuilding the theme cache on every page load (useful if you're a themer working on template files)
  • A block that'll provide easy access to the empty cache function and reinstall function.

With the Devel module and caching turned off under Performance, the only other major cache you'll run into in routine development is the menu router, which can be rebuilt either using Devel or Admin Menu as Steve mentioned.

4

One important thing to note is that if you have a lot of stylesheets (either from your theme or from modules), then turning off CSS file optimizing has the potential to break your site in IE.

While not actually caching, it's on the same settings page and is often used with caching.

The "Flush all caches" link in the admin menu module is fantastic, by the way.

3

If you're using Drupal 6, the "Cache Disable" module can be handy for that task if you do not wish [for some reason] to install heavier modules like Devel.

2

Old question, but I just saw it popped up as a related one to a search I was doing.

Nearly all of the cache clearing methods end up calling drupal_flush_all_caches in the end. You can play around with calling this in various circumstances in your own code.

2

Amount of caches used with Drupal sometimes can get crazy.

Here are the common methods of disabling and clearing Drupal caches and associated with it.


DISABLING CACHES:

Note that you can't disable all the caches, as some of them are required by Drupal.

Here is a method of disabling the caches by adding the following lines into your settings file:

// Disable caches
$conf['cache'] = 0;
$conf['block_cache'] = 0;
$conf['views_skip_cache'] = TRUE;
$conf['page_compression'] = 0;
$conf['preprocess_css'] = 0;
$conf['preprocess_js'] = 0;

If you disable all your caches, your website will be very slow, so it's not recommended for any other environments exempt your development machine.


CLEARING CACHES

XCache

Clear XCache caches if used:

drush eval "function_exists('xcache_clear_cache') && xcache_clear_cache();"

Or clear the cache in XCache in admin panel (/xcache-admin) if above won't work.

eAccelerator

drush eval "function_exists('accelerator_reset') && accelerator_reset();"

APC

drush eval "function_exists('apc_clear_cache') && apc_clear_cache();"

OPCache

Clear PHP OPCache if using with PHP >=5.5

drush eval "function_exists('opcache_reset') && opcache_reset();"

Note: Above CLI command won't clear cache for Apache, as per: Opcache - Clean cache in PHP5.4 and lower.

memcached

If you're using memcached, you should clear, restart or kill your memcached with one of the methods:

As a user (one of these):

echo flush_all > /dev/tcp/127.0.0.1/11211
echo flush_all | nc localhost 11211 # Flush Contents Of a Memcached Server

As a root (one of these):

/etc/init.d/rc.d/memcached restart
service memcached restart
killall memcached # It's automatically restarted on OSX.

It is also advised to install memcached module with the following line in your settings file:

$conf['cache_backends'][] = 'sites/all/modules/contrib/memcache/memcache.inc';

So Memcached caches are cleared on standard Drupal clear cache.

Varnish

If you're using Varnish, it's advised to install Varnish module and set the following line in your settings file:

$conf['cache_backends'][] = 'sites/all/modules/contrib/varnish/varnish.cache.inc';

So Varnish cache would be cleared together on Drupal clear cache.

Alternatively you may use curl for clearing the pages manually:

curl -X PURGE -H "Host: domain.com" http://127.0.0.1/node/123

Read more: How can I clear the Varnish cache?

Drupal

Clear Drupal caches via drush:

drush cc all
echo "SHOW TABLES LIKE 'cache%'" | $(drush sql-connect) | tail -n +2 | xargs -L1 -I% echo "DELETE FROM %;" | $(drush sql-connect) -v
drush eval 'menu_rebuild();'

You may do the same at /admin/config/development/performance, as sometimes UI caches are cleared in the better way than from CLI.


Here is the useful shell script for clearing all the caches:

#!/bin/bash
set -o xtrace
echo "SHOW TABLES LIKE 'cache%'" | $(drush sql-connect) | tail -n +2 | xargs -L1 -I% echo "DELETE FROM %;" | $(drush sql-connect) -v
echo 'flush_all' | nc localhost 11211 # Flush Contents Of a Memcached Server
drush eval "function_exists('opcache_reset') && opcache_reset(); function_exists('xcache_clear_cache') && xcache_clear_cache();"
drush eval 'menu_rebuild();'
set -o xtrace -
echo done.
0

we can use one of the following methods to clear cache in drupal

1.) We can just visit url your_domain/admin/config/development/performance and click clear cache button.

2.) If We have install drush module we can we clear cache by this drush cc all command.

3.) If We have database access we can clear cache with the following commands (direct access to a production database is emphatically not recommended; use one of the alternatives instead if your site is "live")

TRUNCATE TABLE cache;

TRUNCATE TABLE cache_block;

TRUNCATE TABLE cache_bootstrap;

TRUNCATE TABLE cache_field;

TRUNCATE TABLE cache_filter;

TRUNCATE TABLE cache_form; // note important differences from other cache_* tables -- see more info below

TRUNCATE TABLE cache_image;

TRUNCATE TABLE cache_menu;

TRUNCATE TABLE cache_page;

TRUNCATE TABLE cache_path;

TRUNCATE TABLE cache_token;

TRUNCATE TABLE cache_update;

4.) We can also write query in our module file db_query("DELETE FROM {cache};");

5.) We can use drupal api for clearing cache like cache_clear_all()

6.) We can also clear cache by devel module install devel module and enable the "Developer/Development Block" by this we can clear cache

More info on cache_form, which has a different purpose from other cache_ tables:

  • core api docs for drupal_flush_all_caches() say "Don't clear cache_form - in-progress form submissions may break." This concern applies only to sites with active users and not to development versions.

  • other manual methods of clearing caches described in this post will not affect cache_form

  • if you need to shrink the size of cache_form on a production site, see Cache Form table size is enormous

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.