do_action( ‘delete_attachment’, int $post_id, WP_Post $post )

Fires before an attachment is deleted, at the start of wp_delete_attachment() .

Parameters

$post_idint
Attachment ID.
$postWP_Post
Post object.

More Information

Up to and including WordPress 2.7, it is fired ”after” the attachment is deleted from the database and the file system, limiting its usefulness. As of changeset #10400 (WordPress 2.8), the action will fire ”before” anything is deleted.

Source

do_action( 'delete_attachment', $post_id, $post );

Changelog

VersionDescription
5.5.0Added the $post parameter.
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Below is the basic example how delete_attachment action works.

    function wpdocs_delete_attachment( $post_id ) {
    	// Do something before attachment deleted
    
    	// get attached file dir
    	$file = get_attached_file( $post_id );
    
    	// get file name of attachment to be deleted
    	$file_name = wp_basename( $file );
    }
    add_action( 'delete_attachment', 'wpdocs_delete_attachment' );

You must log in before being able to contribute a note or feedback.