Using WordPress ‘delete_{$taxonomy}’ PHP action

The delete_{$taxonomy} WordPress PHP action fires after a term in a specific taxonomy is deleted. The dynamic portion of the hook name, $taxonomy, refers to the specific taxonomy the term belonged to. Possible hook names include delete_category and delete_post_tag.

Usage

add_action('delete_{$taxonomy}', 'your_custom_function', 10, 4);

function your_custom_function($term, $tt_id, $deleted_term, $object_ids) {
  // your custom code here
}

Parameters

  • $term (int) – Term ID.
  • $tt_id (int) – Term taxonomy ID.
  • $deleted_term (WP_Term) – Copy of the already-deleted term.
  • $object_ids (array) – List of term object IDs.

More information

See WordPress Developer Resources: delete_{$taxonomy}

Examples

Log term deletion

Log the deleted term ID and taxonomy in a custom log file.

add_action('delete_category', 'log_term_deletion', 10, 4);

function log_term_deletion($term, $tt_id, $deleted_term, $object_ids) {
  error_log("Term ID: {$term}, Taxonomy: {$deleted_term->taxonomy} has been deleted.");
}

Update related post meta

Remove the term ID from related post meta on term deletion.

add_action('delete_post_tag', 'update_related_post_meta', 10, 4);

function update_related_post_meta($term, $tt_id, $deleted_term, $object_ids) {
  foreach ($object_ids as $post_id) {
    $related_terms = get_post_meta($post_id, 'related_terms', true);
    if (($key = array_search($term, $related_terms)) !== false) {
      unset($related_terms[$key]);
      update_post_meta($post_id, 'related_terms', $related_terms);
    }
  }
}

Notify admin on term deletion

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

add_action('delete_category', 'notify_admin_on_term_deletion', 10, 4);

function notify_admin_on_term_deletion($term, $tt_id, $deleted_term, $object_ids) {
  $admin_email = get_option('admin_email');
  $subject = "Term Deleted: {$deleted_term->name}";
  $message = "The term '{$deleted_term->name}' (ID: {$term}) in the '{$deleted_term->taxonomy}' taxonomy has been deleted.";
  wp_mail($admin_email, $subject, $message);
}

Delete term thumbnail

Delete the term thumbnail from the media library when a term is deleted.

add_action('delete_post_tag', 'delete_term_thumbnail', 10, 4);

function delete_term_thumbnail($term, $tt_id, $deleted_term, $object_ids) {
  $thumbnail_id = get_term_meta($term, 'thumbnail_id', true);
  if ($thumbnail_id) {
    wp_delete_attachment($thumbnail_id, true);
  }
}

Track term deletions

Add a custom post type entry to track term deletions.

add_action('delete_category', 'track_term_deletions', 10, 4);

function track_term_deletions($term, $tt_id, $deleted_term, $object_ids) {
  $log_data = array(
    'post_title' => "Term Deleted: {$deleted_term->name}",
    'post_type' => 'term_deletion_log',
    'post_status' => 'publish',
  );
  $log_id = wp_insert_post($log_data);

  update_post_meta($log_id, 'term_id', $term);
  update_post_meta($log_id, 'taxonomy', $deleted_term->taxonomy);
}