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 (invoking hook_cache_flush()), which always includes:

    • 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

hook_cache_flush()

hook_rebuild()

9 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.
system_clear_cache_submit in drupal/core/modules/system/system.admin.inc
Submit callback; clear system 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 6588
Common functions that many Drupal modules will need to reference.

Code

function drupal_flush_all_caches() {

  // 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.
  foreach (module_invoke_all('cache_flush') as $bin) {
    cache($bin)
      ->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.
  // None currently; kept if any static caches need to be reset in the future.
  // Rebuild module and theme data.
  system_rebuild_module_data();
  system_rebuild_theme_data();

  // Ensure that all modules that are currently supposed to be enabled are
  // actually loaded.
  module_load_all();

  // Update the list of bootstrap modules.
  // Allows developers to get new hook_boot() 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_invoke_all('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_container()
    ->get('router.builder')
    ->rebuild();
  menu_router_rebuild();

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

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