function 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 'cache' 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()

8 calls to drupal_flush_all_caches()
ClearTest::testFlushAllCaches in drupal/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php
Tests drupal_flush_all_caches().
config_admin_import_form_submit in drupal/core/modules/config/config.admin.inc
Form submission handler for config_admin_import_form().
install_finished in drupal/core/includes/install.core.inc
Performs final installation steps and displays a 'finished' page.
PerformanceForm::submitCacheClear in drupal/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php
Clears the caches.
system_modules_submit in drupal/core/modules/system/system.admin.inc
Submit callback; handles modules form submission.

... See full list

File

drupal/core/includes/common.inc, line 5589
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) {
    if ($service_id != 'cache.menu') {
      $cache_backend
        ->deleteAll();
    }
  }

  // Flush asset file caches.
  drupal_clear_css_cache();
  drupal_clear_js_cache();
  _drupal_flush_css_js();

  // Reset all static caches.
  drupal_static_reset();

  // Clear all non-drupal_static() static caches.
  Drupal::entityManager()
    ->clearCachedDefinitions();

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

  // Rebuild module and theme data.
  $module_data = system_rebuild_module_data();
  system_rebuild_theme_data();

  // 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 $module => $data) {
    if (isset($data->uri) && $data->status) {
      $files[$module] = $data->uri;
    }
  }
  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();

  // Update the list of bootstrap modules.
  // Allows developers to get new bootstrap hooks implementations registered
  // without having to write a hook_update_N() function.
  _system_update_bootstrap_status();

  // Rebuild the schema and cache a fully-built schema based on new module data.
  // This is necessary for any invocation of index.php, because setting cache
  // table entries requires schema information and that occurs during bootstrap
  // before any modules are loaded, so if there is no cached schema,
  // drupal_get_schema() will try to generate one, but with no loaded modules,
  // it will return nothing.
  drupal_get_schema(NULL, TRUE);

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

  // 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();
  menu_router_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')) {
    unset($GLOBALS['theme']);
    drupal_maintenance_theme();
  }
}