Using WordPress ‘delete_option’ PHP action

The delete_option WordPress PHP action fires immediately before an option is deleted.

Usage

add_action('delete_option', 'your_function_name', 10, 1);

function your_function_name($option) {
  // Your custom code here
}

Parameters

  • $option: string – Name of the option to delete.

More information

See WordPress Developer Resources: delete_option

Examples

Log deleted option

Log the option name when it’s deleted.

add_action('delete_option', 'log_deleted_option', 10, 1);

function log_deleted_option($option) {
  error_log("Deleted option: " . $option);
}

Prevent deletion of specific option

Prevent the deletion of the ‘my_custom_option’ option.

add_action('delete_option', 'prevent_custom_option_deletion', 10, 1);

function prevent_custom_option_deletion($option) {
  if ($option == 'my_custom_option') {
    wp_die("You cannot delete 'my_custom_option'.");
  }
}

Backup option before deletion

Backup the option value to a text file before it’s deleted.

add_action('delete_option', 'backup_option_before_delete', 10, 1);

function backup_option_before_delete($option) {
  $option_value = get_option($option);
  file_put_contents($option . "_backup.txt", $option_value);
}

Notify admin about deleted option

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

add_action('delete_option', 'notify_admin_about_deleted_option', 10, 1);

function notify_admin_about_deleted_option($option) {
  if ($option == 'important_option') {
    $admin_email = get_option('admin_email');
    wp_mail($admin_email, 'Important option deleted', 'The important_option has been deleted.');
  }
}

Clear cache on option deletion

Clear the cache when an option related to cache settings is deleted.

add_action('delete_option', 'clear_cache_on_option_deletion', 10, 1);

function clear_cache_on_option_deletion($option) {
  if (strpos($option, 'cache_settings_') === 0) {
    // Clear cache code here
  }
}