Using WordPress ‘deleted_blog’ PHP action

The deleted_blog WordPress PHP action is triggered after a site is deleted from a WordPress multisite network.

Usage

add_action('deleted_blog', 'your_custom_function', 10, 2);

function your_custom_function($site_id, $drop) {
    // your custom code here
}

Parameters

  • $site_id (int) – The ID of the site being deleted.
  • $drop (bool) – True if the site’s tables should be dropped. Default is false.

More information

See WordPress Developer Resources: deleted_blog

Examples

Log site deletion

Log the deletion of a site to a custom log file.

add_action('deleted_blog', 'log_site_deletion', 10, 2);

function log_site_deletion($site_id, $drop) {
    // Log site deletion in a custom log file
    $log_message = "Site ID: {$site_id} was deleted on " . date('Y-m-d H:i:s') . "\n";
    error_log($log_message, 3, "/path/to/your/custom/log/file.log");
}

Send email notification

Send an email notification to the network admin when a site is deleted.

add_action('deleted_blog', 'send_site_deletion_notification', 10, 2);

function send_site_deletion_notification($site_id, $drop) {
    $to = get_network_admin_email();
    $subject = 'Site Deleted';
    $message = "Site with ID {$site_id} was deleted.";
    wp_mail($to, $subject, $message);
}

Remove site-specific options

Remove site-specific options from a global options table when a site is deleted.

add_action('deleted_blog', 'remove_site_options', 10, 2);

function remove_site_options($site_id, $drop) {
    global $wpdb;
    $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}global_options WHERE site_id = %d", $site_id));
}

Clean up custom table

Delete entries related to the deleted site from a custom table.

add_action('deleted_blog', 'clean_custom_table', 10, 2);

function clean_custom_table($site_id, $drop) {
    global $wpdb;
    $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}your_custom_table WHERE site_id = %d", $site_id));
}

Custom action upon site deletion

Perform a custom action when a site is deleted, like updating an external API.

add_action('deleted_blog', 'custom_site_deletion_action', 10, 2);

function custom_site_deletion_action($site_id, $drop) {
    // Your custom code to update an external API or perform other actions
}