Using WordPress ‘clean_network_cache’ PHP action

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

Usage

add_action('clean_network_cache', 'your_custom_function');
function your_custom_function($id) {
    // your custom code here
}

Parameters

  • $id: int, the Network ID.

More information

See WordPress Developer Resources: clean_network_cache

Examples

Clearing Transients for a Network

Clear all transients for a specific network when the network cache is cleaned.

add_action('clean_network_cache', 'clear_network_transients');
function clear_network_transients($network_id) {
    // Get all transients for the network
    $transients = get_network_option($network_id, '_transient_prefix');

    // Loop through and delete each transient
    foreach ($transients as $transient) {
        delete_network_transient($network_id, $transient);
    }
}

Logging Network Cache Clean

Log when the network cache is cleaned.

add_action('clean_network_cache', 'log_network_cache_clean');
function log_network_cache_clean($network_id) {
    // Log the cache clean event
    error_log("Network cache cleaned for network ID: {$network_id}");
}

Invalidate Cached Network Data

Invalidate cached network data when the network cache is cleaned.

add_action('clean_network_cache', 'invalidate_cached_network_data');
function invalidate_cached_network_data($network_id) {
    // Invalidate the cache for the specific network
    wp_cache_delete("network_data_{$network_id}");
}

Notify Admin on Network Cache Clean

Send an email notification to the admin when the network cache is cleaned.

add_action('clean_network_cache', 'notify_admin_network_cache_clean');
function notify_admin_network_cache_clean($network_id) {
    // Get admin email
    $admin_email = get_network_option($network_id, 'admin_email');

    // Send email notification
    wp_mail($admin_email, 'Network Cache Cleaned', "The network cache has been cleaned for network ID: {$network_id}");
}

Update Network Cleanup Timestamp

Update a network option with the timestamp of the last network cache clean.

add_action('clean_network_cache', 'update_network_cleanup_timestamp');
function update_network_cleanup_timestamp($network_id) {
    // Update the network option with the current timestamp
    update_network_option($network_id, 'last_cache_clean', time());
}