Using WordPress ‘delete_post’ PHP action

The delete_post WordPress PHP action is triggered right before a post is deleted from the database.


add_action('delete_post', 'your_custom_function_name');
function your_custom_function_name($post_id) {
  // your custom code here


  • $post_id (int) – The ID of the post being deleted.

More information

See WordPress Developer Resources: delete_post


Log post deletion

Log the deletion of a post in a separate log file.

add_action('delete_post', 'log_post_deletion');
function log_post_deletion($post_id) {
  // Log the post ID to a file
  error_log("Deleted post ID: {$post_id}\n", 3, '/path/to/your/logs/post_deletions.log');

Delete associated meta data

Delete associated custom meta data when a post is deleted.

add_action('delete_post', 'delete_post_meta_data');
function delete_post_meta_data($post_id) {
  // Delete custom meta data with 'custom_meta_key'
  delete_post_meta($post_id, 'custom_meta_key');

Send an email notification

Send an email notification to the site administrator when a post is deleted.

add_action('delete_post', 'send_email_on_post_deletion');
function send_email_on_post_deletion($post_id) {
  $admin_email = get_option('admin_email');
  $subject = "Post Deleted: {$post_id}";
  $message = "A post with ID {$post_id} was deleted.";
  wp_mail($admin_email, $subject, $message);

Delete related attachments when a post is deleted.

add_action('delete_post', 'delete_related_attachments');
function delete_related_attachments($post_id) {
  $attachments = get_attached_media('image', $post_id);
  foreach ($attachments as $attachment) {
    wp_delete_attachment($attachment->ID, true);

Remove post from custom cache

Remove a post from a custom cache when it is deleted.

add_action('delete_post', 'remove_post_from_custom_cache');
function remove_post_from_custom_cache($post_id) {
  // Remove the post from your custom cache
  your_custom_cache_function('remove', $post_id);