wp_delete_attachment_files()WP 4.9.7

Deletes all files associated with the specified attachment.

The function does not delete posts from the database, but only removes files in the uploads folder. That is, if it is applied to a media file attachment, the record in the admin table will remain, but the links to the associated files will be broken.

No Hooks.

Returns

bool. True on success, false on failure.

Usage

wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
$post_id(integer) (required)
ID of the attachment.
$meta(array) (required)
Metadata of the attachment.
$backup_sizes(array) (required)
Metadata of the backup files of the attachment.
$file(string) (required)
Absolute path to the attachment file.

Examples

#1 Deleting all files associated with an attachment that has id 12

$attach_id    = 12;
$meta         = wp_get_attachment_metadata( $attach_id );
$backup_sizes = get_post_meta( $attach_id, '_wp_attachment_backup_sizes', true );
$file         = get_attached_file( $attach_id );

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

#2 Delete attachment files, except thumbnail files from the upload directory

$attach_id    = 12;
$meta         = false;
$backup_sizes = get_post_meta( $attach_id, '_wp_attachment_backup_sizes', true );
$file         = get_attached_file( $attach_id );

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

Notes

Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 4.9.7 Introduced.

wp_delete_attachment_files() code WP 7.1

function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
	global $wpdb;

	$uploadpath = wp_get_upload_dir();
	$deleted    = true;

	if ( ! empty( $meta['thumb'] ) ) {
		// Don't delete the thumb if another attachment uses it.
		if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
			$thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );

			if ( ! empty( $thumbfile ) ) {
				$thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
				$thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );

				if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
					$deleted = false;
				}
			}
		}
	}

	// Remove intermediate and backup images if there are any.
	if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
		$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );

		foreach ( $meta['sizes'] as $size => $sizeinfo ) {
			$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );

			if ( ! empty( $intermediate_file ) ) {
				$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );

				if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
					$deleted = false;
				}
			}
		}
	}

	if ( ! empty( $meta['original_image'] ) ) {
		if ( empty( $intermediate_dir ) ) {
			$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
		}

		$original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );

		if ( ! empty( $original_image ) ) {
			$original_image = path_join( $uploadpath['basedir'], $original_image );

			if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
				$deleted = false;
			}
		}
	}

	/*
	 * Delete the source-format companion file. The client-side media flow can
	 * sideload a source-format original (such as a HEIC file) alongside a
	 * web-viewable derivative, recording its filename under the 'source_image'
	 * key. This is kept separate from 'original_image', which continues to
	 * point at the derivative.
	 */
	if ( ! empty( $meta['source_image'] ) && is_string( $meta['source_image'] ) ) {
		if ( empty( $intermediate_dir ) ) {
			$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
		}

		$source_image = str_replace( wp_basename( $file ), $meta['source_image'], $file );

		if ( ! empty( $source_image ) ) {
			$source_image = path_join( $uploadpath['basedir'], $source_image );

			if ( ! wp_delete_file_from_directory( $source_image, $intermediate_dir ) ) {
				$deleted = false;
			}
		}
	}

	/*
	 * Delete the animated-GIF video companions. When the client-side media flow
	 * converts an opaque animated GIF to a web-safe video, the converted MP4/WebM
	 * and a static first-frame JPEG poster are sideloaded alongside the GIF and
	 * recorded under the 'animated_video' and 'animated_video_poster' keys. These
	 * are kept separate from 'original_image', which continues to point at the GIF.
	 */
	foreach ( array( 'animated_video', 'animated_video_poster' ) as $companion_key ) {
		if ( empty( $meta[ $companion_key ] ) || ! is_string( $meta[ $companion_key ] ) ) {
			continue;
		}

		if ( empty( $intermediate_dir ) ) {
			$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
		}

		$companion_file = str_replace( wp_basename( $file ), $meta[ $companion_key ], $file );

		if ( ! empty( $companion_file ) ) {
			$companion_file = path_join( $uploadpath['basedir'], $companion_file );

			if ( ! wp_delete_file_from_directory( $companion_file, $intermediate_dir ) ) {
				$deleted = false;
			}
		}
	}

	if ( is_array( $backup_sizes ) ) {
		$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );

		foreach ( $backup_sizes as $size ) {
			$del_file = path_join( dirname( $meta['file'] ), $size['file'] );

			if ( ! empty( $del_file ) ) {
				$del_file = path_join( $uploadpath['basedir'], $del_file );

				if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
					$deleted = false;
				}
			}
		}
	}

	if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
		$deleted = false;
	}

	return $deleted;
}