wp_delete_attachment( int $post_id, bool $force_delete = false ): WP_Post|false|null  Trashes or deletes an attachment.   Description When an attachment is permanently deleted, the file will also be removed.Deletion removes all post meta fields, taxonomy, comments, etc. associated with the attachment (except the main post). The attachment is moved to the Trash instead of permanently deleted unless Trash for media is disabled, item is already in the Trash, or $force_delete is true.   Parameters   $post_id int Required    Attachment ID.    $force_delete bool Optional     Whether to bypass Trash and force deletion.   Default: false     Return WP_Post|false|null Post data on success, false or null on failure.   Source  File: wp-includes/post.php. View all references  function wp_delete_attachment( $post_id, $force_delete = false ) {
	global $wpdb;

	$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );

	if ( ! $post ) {
		return $post;
	}

	$post = get_post( $post );

	if ( 'attachment' !== $post->post_type ) {
		return false;
	}

	if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
		return wp_trash_post( $post_id );
	}

	/**
	 * Filters whether an attachment deletion should take place.
	 *
	 * @since 5.5.0
	 *
	 * @param WP_Post|false|null $delete       Whether to go forward with deletion.
	 * @param WP_Post            $post         Post object.
	 * @param bool               $force_delete Whether to bypass the Trash.
	 */
	$check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete );
	if ( null !== $check ) {
		return $check;
	}

	delete_post_meta( $post_id, '_wp_trash_meta_status' );
	delete_post_meta( $post_id, '_wp_trash_meta_time' );

	$meta         = wp_get_attachment_metadata( $post_id );
	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
	$file         = get_attached_file( $post_id );

	if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) {
		clean_dirsize_cache( $file );
	}

	/**
	 * Fires before an attachment is deleted, at the start of wp_delete_attachment().
	 *
	 * @since 2.0.0
	 * @since 5.5.0 Added the `$post` parameter.
	 *
	 * @param int     $post_id Attachment ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'delete_attachment', $post_id, $post );

	wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) );
	wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) );

	// Delete all for any posts.
	delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );

	wp_defer_comment_counting( true );

	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) );
	foreach ( $comment_ids as $comment_id ) {
		wp_delete_comment( $comment_id, true );
	}

	wp_defer_comment_counting( false );

	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) );
	foreach ( $post_meta_ids as $mid ) {
		delete_metadata_by_mid( 'post', $mid );
	}

	/** This action is documented in wp-includes/post.php */
	do_action( 'delete_post', $post_id, $post );
	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
	if ( ! $result ) {
		return false;
	}
	/** This action is documented in wp-includes/post.php */
	do_action( 'deleted_post', $post_id, $post );

	wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );

	clean_post_cache( $post );

	return $post;
}
   Hooks     do_action( 'deleted_post', int $postid, WP_Post $post )   
Fires immediately after a post is deleted from the database.      do_action( 'delete_attachment', int $post_id, WP_Post $post )   
Fires before an attachment is deleted, at the start of wp_delete_attachment() .      do_action( 'delete_post', int $postid, WP_Post $post )   
Fires immediately before a post is deleted from the database.      apply_filters( 'pre_delete_attachment', WP_Post|false|null $delete, WP_Post $post, bool $force_delete )   
Filters whether an attachment deletion should take place.      Related  Uses    Uses Description      clean_dirsize_cache() wp-includes/functions.php   Cleans directory size cache used by recurse_dirsize() .     wp_delete_attachment_files() wp-includes/post.php   Deletes all files that belong to the given attachment.     delete_metadata() wp-includes/meta.php   Deletes metadata for the specified object.     wp_delete_comment() wp-includes/comment.php   Trashes or deletes a comment.     wp_defer_comment_counting() wp-includes/comment.php   Determines whether to defer comment counting.     wp8b7c:f320:99b9:690f:4595:cd17:293a:c069delete() wp-includes/class-wpdb.php   Deletes a row in the table.     wp8b7c:f320:99b9:690f:4595:cd17:293a:c069get_col() wp-includes/class-wpdb.php   Retrieves one column from the database.     wp8b7c:f320:99b9:690f:4595:cd17:293a:c069get_row() wp-includes/class-wpdb.php   Retrieves one row from the database.     get_attached_file() wp-includes/post.php   Retrieves attached file path based on attachment ID.     delete_metadata_by_mid() wp-includes/meta.php   Deletes metadata by meta ID.     delete_post_meta() wp-includes/post.php   Deletes a post meta field for the given post ID.     wp_trash_post() wp-includes/post.php   Moves a post or page to the Trash     wp_get_attachment_metadata() wp-includes/post.php   Retrieves attachment metadata for attachment ID.     clean_post_cache() wp-includes/post.php   Will clean the post in the cache.     get_object_taxonomies() wp-includes/taxonomy.php   Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.     wp_delete_object_term_relationships() wp-includes/taxonomy.php   Unlinks the object from the taxonomy or taxonomies.     get_post() wp-includes/post.php   Retrieves post data given a post ID or post object.     do_action() wp-includes/plugin.php   Calls the callback functions that have been added to an action hook.     apply_filters() wp-includes/plugin.php   Calls the callback functions that have been added to a filter hook.     wp8b7c:f320:99b9:690f:4595:cd17:293a:c069prepare() wp-includes/class-wpdb.php   Prepares a SQL query for safe execution.     is_multisite() wp-includes/load.php   If Multisite is enabled.     get_post_meta() wp-includes/post.php   Retrieves a post meta field for the given post ID.   
   Used By    Used By Description      wp_ajax_media_create_image_subsizes() wp-admin/includes/ajax-actions.php   Ajax handler for creating missing image sub-sizes for just uploaded images.     File_Upload_Upgrader8b7c:f320:99b9:690f:4595:cd17:293a:c069leanup() wp-admin/includes/class-file-upload-upgrader.php   Delete the attachment/uploaded file.     wp_import_cleanup() wp-admin/includes/import.php   Cleanup importer.     wp_delete_post() wp-includes/post.php   Trashes or deletes a post or page.   
    Changelog    Version Description     2.0.0 Introduced.    

    
{'path': 'public/docs/wordpress/functions/wp_delete_attachment.html', 'source': 'DevDocs.IO'}