After enabling Redis, old cache tables remain populated

Created on 16 October 2020, over 4 years ago
Updated 19 October 2024, 6 months ago

Problem/Motivation

After Redis is enabled, the cache_* and cachetags tables remain full of data that is no longer used. On high-traffic sites before Redis was enabled, it could be a large amount of data.

Steps to reproduce

After enabling Redis, note the number of rows in the cache tables.

They will not increase or decrease, even when cache is cleared. If they are emptied, they will not grow again until the cache backend is set back to the Database.

I ran these commands in MySQL to clear the cache tables manually:

TRUNCATE cache_bootstrap;
TRUNCATE cache_config;
TRUNCATE cache_container;
TRUNCATE cache_data;
TRUNCATE cache_default;
TRUNCATE cache_discovery;
TRUNCATE cache_entity;
TRUNCATE cache_library;
TRUNCATE cache_menu;
TRUNCATE cache_page;
TRUNCATE cache_render;
TRUNCATE cache_rest;
TRUNCATE cache_toolbar;
TRUNCATE cachetags;

But I think the tables that are cached could vary from site to site as some sites have more or less cache buckets.

Proposed resolution

A few ideas:

- Maybe cache clear should clear the cache database tables anyways.
- Add instructions to the install to manually clear the tables via MySQL.

Maybe there's a better solution?

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.4

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States dandaman

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain facine

    @deviantintegral, I solved this other issue with πŸ› After enabling Redis, old cache tables remain populated Active

    /**
     * Implements hook_rebuild().
     *
     * @see https://www.drupal.org/project/redis/issues/3177375
     */
    function hook_rebuild() {
      // When redis is enabled, old cache tables are not truncated.
      if (!extension_loaded('redis')) {
        return;
      }
    
      $database = Drupal::database();
      $schema = $database->schema();
      foreach (Cache::getBins() as $name => $bin) {
        if (!$bin instanceof DatabaseBackend &&
          $schema->tableExists('cache_' . $name)) {
    
          $database->truncate('cache_' . $name)->execute();
        }
      }
    
      if (!\Drupal::service('cache_tags.invalidator.checksum') instanceof DatabaseCacheTagsChecksum &&
        $schema->tableExists('cachetags')) {
    
        $database->truncate('cachetags')->execute();
      }
    } 
    
Production build 0.71.5 2024