You are here

function drupal_flush_all_caches in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/common.inc \drupal_flush_all_caches()

Flushes all persistent caches, resets all variables, and rebuilds all data structures.

At times, it is necessary to re-initialize the entire system to account for changed or new code. This function:

  • Clears all persistent caches:

    • The bootstrap cache bin containing base system, module system, and theme system information.
    • The common 'default' cache bin containing arbitrary caches.
    • The page cache.
    • The URL alias path cache.
  • Resets all static variables that have been defined via drupal_static().
  • Clears asset (JS/CSS) file caches.
  • Updates the system with latest information about extensions (modules and themes).
  • Updates the bootstrap flag for modules implementing bootstrap_hooks().
  • Rebuilds the full database schema information (invoking hook_schema()).
  • Rebuilds data structures of all modules (invoking hook_rebuild()). In core this means

    • blocks, node types, date formats and actions are synchronized with the database
    • The 'active' status of fields is refreshed.
  • Rebuilds the menu router.

This means the entire system is reset so all caches and static variables are effectively empty. After that is guaranteed, information about the currently active code is updated, and rebuild operations are successively called in order to synchronize the active system according to the current information defined in code.

All modules need to ensure that all of their caches are flushed when hook_cache_flush() is invoked; any previously known information must no longer exist. All following hook_rebuild() operations must be based on fresh and current system data. All modules must be able to rely on this contract.

This function also resets the theme, which means it is not initialized anymore and all previously added JavaScript and CSS is gone. Normally, this function is called as an end-of-POST-request operation that is followed by a redirect, so this effect is not visible. Since the full reset is the whole point of this function, callers need to take care for backing up all needed variables and properly restoring or re-initializing them on their own. For convenience, this function automatically re-initializes the maintenance theme if it was initialized before.

@todo Try to clear page/JS/CSS caches last, so cached pages can still be served during this possibly long-running operation. (Conflict on bootstrap cache though.) @todo Add a global lock to ensure that caches are not primed in concurrent requests.

See also

\Drupal\Core\Cache\CacheHelper::getBins()

hook_cache_flush()

hook_rebuild()

11 calls to drupal_flush_all_caches()
BrowserTestBase::resetAll in core/modules/simpletest/src/BrowserTestBase.php
Resets all data structures after having enabled new modules.
ClearTest::testFlushAllCaches in core/modules/system/src/Tests/Cache/ClearTest.php
Tests drupal_flush_all_caches().
DbUpdateController::batchFinished in core/modules/system/src/Controller/DbUpdateController.php
Finishes the update process and stores the results for eventual display.
DbUpdateController::selection in core/modules/system/src/Controller/DbUpdateController.php
Renders a list of available database updates.
drupal_rebuild in core/includes/utility.inc
Rebuilds all caches even when Drupal itself does not work.

... See full list

1 string reference to 'drupal_flush_all_caches'
DbUpdateController::triggerBatch in core/modules/system/src/Controller/DbUpdateController.php
Starts the database update batch process.

File

core/includes/common.inc, line 1082
Common functions that many Drupal modules will need to reference.

Code

function drupal_flush_all_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();
  }
}