Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 18324)
+++ wp-includes/post.php	(working copy)
@@ -3733,10 +3733,6 @@
 	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() )
 		delete_transient( 'dirsize_cache' );
 
@@ -3767,38 +3763,32 @@
 	$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id ));
 	do_action( 'deleted_post', $post_id );
 
-	$uploadpath = wp_upload_dir();
+	// Delete all the associated files
+	$meta = wp_get_attachment_metadata( $post_id );
+	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
+	$file = $meta['file'];
 
 	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", '%' . $meta['thumb'] . '%', $post_id)) ) {
-			$thumbfile = str_replace(basename($file), $meta['thumb'], $file);
-			$thumbfile = apply_filters('wp_delete_file', $thumbfile);
-			@ unlink( path_join($uploadpath['basedir'], $thumbfile) );
+		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", '%' . $meta['thumb'] . '%', $post_id)) ) {
+			wp_delete_file( path_join( dirname($file), $meta['thumb'] ) );
 		}
 	}
 
-	// remove intermediate and backup images if there are any
 	foreach ( get_intermediate_image_sizes() as $size ) {
 		if ( $intermediate = image_get_intermediate_size($post_id, $size) ) {
-			$intermediate_file = apply_filters('wp_delete_file', $intermediate['path']);
-			@ unlink( path_join($uploadpath['basedir'], $intermediate_file) );
+			wp_delete_file( $intermediate['path'] );
 		}
 	}
 
-	if ( is_array($backup_sizes) ) {
+	if ( is_array( $backup_sizes ) ) {
 		foreach ( $backup_sizes as $size ) {
-			$del_file = path_join( dirname($meta['file']), $size['file'] );
-			$del_file = apply_filters('wp_delete_file', $del_file);
-			@ unlink( path_join($uploadpath['basedir'], $del_file) );
+			wp_delete_file( path_join( dirname($file), $size['file'] ) );
 		}
 	}
 
-	$file = apply_filters('wp_delete_file', $file);
+	wp_delete_file( $file );
 
-	if ( ! empty($file) )
-		@ unlink($file);
-
 	clean_post_cache($post_id);
 
 	return $post;
Index: wp-includes/media.php
===================================================================
--- wp-includes/media.php	(revision 18324)
+++ wp-includes/media.php	(working copy)
@@ -669,6 +669,22 @@
 }
 
 /**
+ * Applies the 'wp_delete_file' filter before deleting the file
+ *
+ * @since 3.3
+ *
+ * @param string $file A file path, absolute or relative to the wp-uploads directory
+ */
+function wp_delete_file( $file ) {
+	$file = apply_filters( 'wp_delete_file', $file );
+
+	if ( !empty( $file ) ) {
+		$wud = wp_upload_dir();
+		@unlink( path_join( $wud['basedir'], $file ) );
+	}
+}
+
+/**
  * Adds a 'wp-post-image' class to post thumbnail thumbnails
  * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to
  * dynamically add/remove itself so as to only filter post thumbnail thumbnails
@@ -1438,4 +1454,4 @@
 	return apply_filters( 'embed_googlevideo', '<embed type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docid=' . esc_attr($matches[2]) . '&amp;hl=en&amp;fs=true" style="width:' . esc_attr($width) . 'px;height:' . esc_attr($height) . 'px" allowFullScreen="true" allowScriptAccess="always" />', $matches, $attr, $url, $rawattr );
 }
 
-?>
\ No newline at end of file
+?>
Index: wp-admin/includes/image-edit.php
===================================================================
--- wp-admin/includes/image-edit.php	(revision 18324)
+++ wp-admin/includes/image-edit.php	(working copy)
@@ -440,8 +440,7 @@
 			if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
 				// delete only if it's edited image
 				if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {
-					$delpath = apply_filters('wp_delete_file', $file);
-					@unlink($delpath);
+					wp_delete_file( $file );
 				}
 			} else {
 				$backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
@@ -465,8 +464,7 @@
 				if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
 					// delete only if it's edited image
 					if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {
-						$delpath = apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );
-						@unlink($delpath);
+						wp_delete_file( path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );
 					}
 				} else {
 					$backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
@@ -656,8 +654,7 @@
 	}
 
 	if ( $delete ) {
-		$delpath = apply_filters('wp_delete_file', $new_path);
-		@unlink($delpath);
+		wp_delete_file( $new_path );
 	}
 
 	imagedestroy($img);
Index: wp-admin/custom-header.php
===================================================================
--- wp-admin/custom-header.php	(revision 18324)
+++ wp-admin/custom-header.php	(working copy)
@@ -722,7 +722,7 @@
 		}
 
 		$attachment_id = absint( $_POST['attachment_id'] );
-		$original = get_attached_file($attachment_id);
+		$original = get_attached_file( $attachment_id );
 
 		$cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
 		if ( is_wp_error( $cropped ) )
@@ -751,10 +751,10 @@
 
 		set_theme_mod('header_image', $url);
 
-		// cleanup
-		$medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
-		@unlink( apply_filters( 'wp_delete_file', $medium ) );
-		@unlink( apply_filters( 'wp_delete_file', $original ) );
+		// Delete old files
+		$medium = path_join( dirname($original), 'midsize-' . basename($original) );
+		wp_delete_file( $medium );
+		wp_delete_file( $original );
 
 		return $this->finished();
 	}
