11

In Drupal 8, we have drush cr which is the new version of drush cc all now deprecated.

We also had drush cc menu and some more specific commands to clear cache clear. If I try to execute them, I get some weird Symfony error.

What command should I use instead of drush cc <cache name> in Drupal 8?

The documentation for drush cache-clear doesn't help much.

I tried drush cr menu, for example, and it doesn't seem to work.

1
  • What weird error? drush cc and many of its more specific cache clear commands still exist, for example drush cc render.
    – Berdir
    Mar 10, 2017 at 15:08

4 Answers 4

10

drush is not ready for that right now (as far as I know), so you'll need to write custom drush-scripts or do some other execution. drush cr rebuilds the whole cache, drush cc is not working with drupal8.

the method drupal_flush_all_caches shows how to flush all core caches.

$module_handler = \Drupal::moduleHandler();
  // Flush all persistent caches.
  // This is executed based on old/previously known information, which is
  // sufficient, since new extensions cannot have any primed caches yet.
  $module_handler->invokeAll('cache_flush');
  foreach (Cache::getBins() as $service_id => $cache_backend) {
    $cache_backend->deleteAll();
  }

  // Flush asset file caches.
  \Drupal::service('asset.css.collection_optimizer')->deleteAll();
  \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  _drupal_flush_css_js();

  // Reset all static caches.
  drupal_static_reset();

  // Invalidate the container.
  \Drupal::service('kernel')->invalidateContainer();

  // Wipe the Twig PHP Storage cache.
  PhpStorageFactory::get('twig')->deleteAll();

  // Rebuild module and theme data.
  $module_data = system_rebuild_module_data();
  /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  $theme_handler = \Drupal::service('theme_handler');
  $theme_handler->refreshInfo();
  // In case the active theme gets requested later in the same request we need
  // to reset the theme manager.
  \Drupal::theme()->resetActiveTheme();


  // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
  // sufficient, since the list of enabled modules might have been adjusted
  // above due to changed code.
  $files = array();
  foreach ($module_data as $name => $extension) {
    if ($extension->status) {
      $files[$name] = $extension;
    }
  }
  \Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);
  // New container, new module handler.
  $module_handler = \Drupal::moduleHandler();

  // Ensure that all modules that are currently supposed to be enabled are
  // actually loaded.
  $module_handler->loadAll();

  // Rebuild all information based on new module data.
  $module_handler->invokeAll('rebuild');

  // Clear all plugin caches.
  \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();

  // Rebuild the menu router based on all rebuilt data.
  // Important: This rebuild must happen last, so the menu router is guaranteed
  // to be based on up to date information.
  \Drupal::service('router.builder')->rebuild();

  // Re-initialize the maintenance theme, if the current request attempted to
  // use it. Unlike regular usages of this function, the installer and update
  // scripts need to flush all caches during GET requests/page building.
  if (function_exists('_drupal_maintenance_theme')) {
    \Drupal::theme()->resetActiveTheme();
    drupal_maintenance_theme();
  }

There's a module too, enable admin_toolbar's admin_toolbar_tools submodule to have it all in the toolbar:

1
  • 1
    Thanks for the answer then :) I guess we will have to wait for it. Until then we can write specific scripts. Mar 10, 2017 at 10:53
5

You can use DrupalConsole cache:rebuild command and use the interactive mode to select from the list the cache to clear.

drupal cache:rebuild

You can also use the command alias cr

drupal cr

If you know the cache you are trying to clear try passing as an argument

drupal cr menu
1
  • 1
    Funny enough, even here at the end of 2019 drush cr does not rebuild all cache. Instead, it rebuilds most. It does not "Flush routing and links cache" as you can do from the toolbar when logged in as admin.
    – Patrick
    Dec 17, 2019 at 21:19
4

Drush cc still (or again?) works as it did in v7 you can do e.g.: drush cc css-js or drush cc render see the link you allready gave: https://drushcommands.com/drush-8x/cache/cache-clear/

if you simply do drush cc you will get a list of options. ('menu' is not one of them)

$ drush cc
Enter a number to choose which cache to clear.
 [0]  :  Cancel         
 [1]  :  drush          
 [2]  :  theme-registry 
 [3]  :  router         
 [4]  :  css-js         
 [5]  :  module-list    
 [6]  :  theme-list     
 [7]  :  render         
 [8]  :  token          
 [9]  :  views
1

Use Clear Specific Cache(https://www.drupal.org/project/ccos) module.

The Clear Specific Cache module will help you to clear the cache of sole/specific entity type (i.e block, node, views block, view page, etc.) from contextual links, local task menu, and operations drop-button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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