Using WordPress ‘clean_site_cache’ PHP action

The clean_site_cache WordPress PHP action fires immediately after a site has been removed from the object cache.

Usage

add_action('clean_site_cache', 'my_custom_function', 10, 3);

function my_custom_function($id, $blog, $domain_path_key) {
    // your custom code here
}

Parameters

  • $id (string): Site ID as a numeric string.
  • $blog (WP_Site): Site object.
  • $domain_path_key (string): md5 hash of domain and path.

More information

See WordPress Developer Resources: clean_site_cache

Examples

Log cache cleaning

Log the site ID when the cache is cleaned.

add_action('clean_site_cache', 'log_cache_clean', 10, 3);

function log_cache_clean($id, $blog, $domain_path_key) {
    error_log("Cache cleaned for site ID: " . $id);
}

Notify admin

Send an email to the admin when the site cache is cleaned.

add_action('clean_site_cache', 'notify_admin_cache_clean', 10, 3);

function notify_admin_cache_clean($id, $blog, $domain_path_key) {
    $admin_email = get_option('admin_email');
    wp_mail($admin_email, 'Site Cache Cleaned', 'The site cache has been cleaned for site ID: ' . $id);
}

Update cache cleaning timestamp

Update a custom option with the timestamp of the last cache cleaning.

add_action('clean_site_cache', 'update_cache_clean_timestamp', 10, 3);

function update_cache_clean_timestamp($id, $blog, $domain_path_key) {
    update_option('last_cache_clean_timestamp', time());
}

Invalidate CDN cache

Invalidate the CDN cache when the site cache is cleaned.

add_action('clean_site_cache', 'invalidate_cdn_cache', 10, 3);

function invalidate_cdn_cache($id, $blog, $domain_path_key) {
    // Call to the CDN API to invalidate the cache
}

Reset custom cache

Reset a custom cache when the site cache is cleaned.

add_action('clean_site_cache', 'reset_custom_cache', 10, 3);

function reset_custom_cache($id, $blog, $domain_path_key) {
    // Code to reset your custom cache
}