Run Code on Drupal Cache Clear

Last modified: 
Thursday, May 12th, 2016

Drupal 7's hook_flush_caches() allows you to register a cache table name to be truncated like this:

function hook_flush_caches() {
  return array('cache_example');
}

If you want to run custom logic, you can register a shutdown function in hook_flush_caches() to fire code immediately after this function completes.

<?php
/**
 * Runs a cache clearing routine other than just truncating cache table.
 */
function _MYMODULE_after_cache_clear() {
  // Add your post cache clear logic here.
}


/**
 * This hook allows you to add the name of a cache table to be truncated
 * whenever the caches are flushed. By registering a shutdown function here,
 * we can fire an event immediately after hook_flush_caches() completes.
 */
function MYMODULE_flush_caches() {
  register_shutdown_function('_MYMODULE_set_cleared');
  return array();
}


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.