The clean_attachment_cache WordPress PHP action fires after the given attachment’s cache is cleaned.
Usage
add_action('clean_attachment_cache', 'my_custom_function', 10, 1);
function my_custom_function($attachment_id) {
// Your custom code here
}
Parameters
$id(int) – Attachment ID.
More information
See WordPress Developer Resources: clean_attachment_cache
Examples
Log cache clean event
Log every time an attachment’s cache is cleaned.
add_action('clean_attachment_cache', 'log_cache_clean', 10, 1);
function log_cache_clean($attachment_id) {
// Log the cache clean event
error_log("Attachment cache cleaned for ID: " . $attachment_id);
}
Update custom cache
Update a custom cache system when an attachment’s cache is cleaned.
add_action('clean_attachment_cache', 'update_custom_cache', 10, 1);
function update_custom_cache($attachment_id) {
// Update your custom cache for the attachment
my_custom_cache_update($attachment_id);
}
Notify admin about cache clean
Send an email notification to the admin when an attachment’s cache is cleaned.
add_action('clean_attachment_cache', 'notify_admin', 10, 1);
function notify_admin($attachment_id) {
// Get the admin email
$admin_email = get_option('admin_email');
// Prepare email content
$subject = "Attachment cache cleaned";
$message = "The cache for attachment ID: " . $attachment_id . " has been cleaned.";
// Send email to admin
wp_mail($admin_email, $subject, $message);
}
Refresh attachment metadata
Refresh attachment metadata when the cache is cleaned.
add_action('clean_attachment_cache', 'refresh_attachment_metadata', 10, 1);
function refresh_attachment_metadata($attachment_id) {
// Get attachment metadata
$metadata = wp_get_attachment_metadata($attachment_id);
// Update attachment metadata
wp_update_attachment_metadata($attachment_id, $metadata);
}
Purge CDN cache
Purge the CDN cache for an attachment when its cache is cleaned.
add_action('clean_attachment_cache', 'purge_cdn_cache', 10, 1);
function purge_cdn_cache($attachment_id) {
// Get the attachment URL
$attachment_url = wp_get_attachment_url($attachment_id);
// Purge CDN cache for the attachment URL
my_cdn_purge_cache($attachment_url);
}