Using WordPress ‘deleted_site_transient’ PHP action

The deleted_site_transient WordPress PHP action fires after a site transient is deleted.

Usage

add_action('deleted_site_transient', 'your_custom_function', 10, 1);

function your_custom_function($transient) {
    // Your custom code here
}

Parameters

  • $transient: string – The deleted transient name.

More information

See WordPress Developer Resources: deleted_site_transient

Examples

Log Deleted Transient

Log deleted transient names in a custom log file.

add_action('deleted_site_transient', 'log_deleted_transient', 10, 1);

function log_deleted_transient($transient) {
    error_log("Deleted site transient: {$transient}", 3, "/path/to/your/logfile.log");
}

Clear Cache on Transient Deletion

Clear a custom cache when a specific transient is deleted.

add_action('deleted_site_transient', 'clear_custom_cache', 10, 1);

function clear_custom_cache($transient) {
    if ($transient === 'my_transient_key') {
        my_custom_cache_clear_function();
    }
}

Send Email Notification

Send an email notification to the admin when a specific transient is deleted.

add_action('deleted_site_transient', 'send_email_notification', 10, 1);

function send_email_notification($transient) {
    if ($transient === 'important_transient_key') {
        wp_mail('[email protected]', 'Important Transient Deleted', "Transient {$transient} has been deleted.");
    }
}

Perform Database Cleanup

Perform custom database cleanup when a specific transient is deleted.

add_action('deleted_site_transient', 'perform_database_cleanup', 10, 1);

function perform_database_cleanup($transient) {
    if ($transient === 'database_cleanup_transient_key') {
        my_custom_database_cleanup_function();
    }
}

Track Transient Deletion

Update a custom post meta value to track transient deletions.

add_action('deleted_site_transient', 'track_transient_deletion', 10, 1);

function track_transient_deletion($transient) {
    if ($transient === 'tracking_transient_key') {
        $current_deletion_count = (int) get_post_meta(1, 'transient_deletion_count', true);
        update_post_meta(1, 'transient_deletion_count', $current_deletion_count + 1);
    }
}