11

I don't fully understand the Clear Cache action. I just click in clear cache Clear All caches in: admin/config/development/performance , then I go to the database to check all cache_... tables and I find that none of them are empty.

Is this normal?
Can I just truncate all cache_... tables?

In fact I just need to clear cache_form. Can I truncate this table?

13

Can I just truncate all cache_... tables?

You should not truncate the "cache_form" table, as it contains the data used from Drupal to validate them; if you delete that table, the form currently being submitted from the user would be invalidated, and the users would need to submit the form again.

There could be some other cache tables that cause a module to act strangely. That is the reason modules that are using extra cache tables (whose name generally starts with "cache_") are supposed to implement hook_flush_cache() to return the cache tables that can be cleared from Drupal, and which is then called with the following code, from drupal_flush_all_caches().

  $core = array('cache', 'cache_path', 'cache_filter', 'cache_bootstrap', 'cache_page');
  $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  foreach ($cache_tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }

drupal_flush_all_caches() is the function called from system_clear_cache_submit(), the submission form handler called when you click on the "Clear all caches" button, in the performance settings page.

During cron tasks, system_cron() clears the cache using the following code.

  $core = array('cache', 'cache_path', 'cache_filter', 'cache_page', 'cache_form', 'cache_menu');
  $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  foreach ($cache_tables as $table) {
    cache_clear_all(NULL, $table);
  }

As the first argument of cache_clear_all() is NULL, the code executed in DrupalDatabaseCache::clear() (Drupal 7) is the following one.

if (variable_get('cache_lifetime', 0)) {
  // We store the time in the current user's $user->cache variable which
  // will be saved into the sessions bin by _drupal_session_write(). We then
  // simulate that the cache was flushed for this user by not returning
  // cached data that was cached before the timestamp.
  $user->cache = REQUEST_TIME;

  $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
  if ($cache_flush == 0) {
    // This is the first request to clear the cache, start a timer.
    variable_set('cache_flush_' . $this->bin, REQUEST_TIME);
  }
  elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime', 0))) {
    // Clear the cache for everyone, cache_lifetime seconds have
    // passed since the first request to clear the cache.
    db_delete($this->bin)
        ->condition('expire', CACHE_PERMANENT, '<>')
        ->condition('expire', REQUEST_TIME, '<')
        ->execute();
    variable_set('cache_flush_' . $this->bin, 0);
  }
}

The code removes only the rows, which are not marked as permanent, and are expired, from the tables returned from hook_flush_caches(), and from various cache tables used from Drupal, including "cache_form." There should not be too much rows in "cache_form"; if that happens, you can reduce the time passed between two consecutive executions of the cron tasks, or execute the following code from a custom module.

cache_clear_all(NULL, 'cache_form');

An alternative is to manually cause the cache to be cleared, using the Devel module, and the menu link it shows.

screenshot

  • my problem is that the cache_form table has a size of 63Mb for a website which has been in production for JUST 2 days. My fear is that this table will be growing without control... – chefnelone Mar 8 '12 at 19:51
  • 1
    If you have cron set up, then expired entries from all caches (including cache_form) should be cleared out each time it runs. – mpdonadio Mar 8 '12 at 21:22
  • 1
    @chefnelone As kiamlaluno explained, if you have your cron properly set up, this table should be getting cleared periodically. – amateur barista Mar 9 '12 at 4:15
  • 2
    @kiamlaluno, reading this answer is very informative and practical, a handy quick reference for cache related thorns. 1+ – amateur barista Mar 9 '12 at 4:17
  • As you said I get cron running and the table got cleared. Thanks. – chefnelone Mar 14 '12 at 15:07
6

If you are cache clearing via the UI, then as soon as the page reloads, then the cache will start filling up again. In other words, the act of refreshing that page causes Drupal to start to cache things again (especially cache_menu).

You can DELETE FROM cache on the various tables safely.

I am also pretty sure that doing a drush cc all will result in fully empty cache tables, too.

  • Thanks, just to be sure I just need to clear cache_form. Can I truncate this table? – chefnelone Mar 8 '12 at 15:53
  • 4
    If you read the code for api.drupal.org/api/drupal/includes%21common.inc/function/… then you will see that cache_form doesn't get cleared and why. If you are positive that this is OK, then you should be able to do this. There have been some historic bugs with cache_form not expiring entries, and growing w/o bound. – mpdonadio Mar 8 '12 at 16:15

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.