Using WordPress ‘delete_option_{$option}’ PHP action

The delete_option_{$option} WordPress PHP action fires after a specific option has been deleted.

Usage

add_action('delete_option_{your_option_name}', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

  • $option (string): Name of the deleted option.

More information

See WordPress Developer Resources: delete_option_{$option}

Examples

Log option deletion

Log the deletion of a custom option named “my_custom_option” to a log file.

add_action('delete_option_my_custom_option', 'log_option_deletion');
function log_option_deletion() {
    // log the deletion of the option to a file
    error_log("my_custom_option deleted at " . date('Y-m-d H:i:s') . "\n", 3, "/path/to/your_log_file.log");
}

Notify admin on option deletion

Send an email notification to the site administrator when the “email_notify_option” is deleted.

add_action('delete_option_email_notify_option', 'notify_admin_on_option_deletion');
function notify_admin_on_option_deletion() {
    // send an email to the admin
    $admin_email = get_option('admin_email');
    wp_mail($admin_email, 'Option Deleted', 'The email_notify_option has been deleted.');
}

Remove associated post meta on option deletion

Delete associated post meta when the “related_post_meta_option” is deleted.

add_action('delete_option_related_post_meta_option', 'remove_associated_post_meta');
function remove_associated_post_meta() {
    // delete the associated post meta
    delete_post_meta_by_key('related_post_meta_key');
}

Clear transient cache on option deletion

Clear the transient cache when the “cache_option” is deleted.

add_action('delete_option_cache_option', 'clear_transient_cache');
function clear_transient_cache() {
    // clear the transient cache
    global $wpdb;
    $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_%'");
}

Delete related user meta when the “related_user_meta_option” is deleted.

add_action('delete_option_related_user_meta_option', 'delete_related_user_meta');
function delete_related_user_meta() {
    // delete the related user meta
    global $wpdb;
    $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'related_user_meta_key'");
}