Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 40036)
+++ src/wp-includes/post.php	(working copy)
@@ -4919,7 +4919,7 @@
 	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(basename($file), $meta['thumb'], $file);
+			$thumbfile = str_replace(wp_basename($file), $meta['thumb'], $file);
 			/** This filter is documented in wp-includes/functions.php */
 			$thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
 			@ unlink( path_join($uploadpath['basedir'], $thumbfile) );
@@ -4928,8 +4928,9 @@
 
 	// Remove intermediate and backup images if there are any.
 	if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
+		$basename_file = wp_basename( $file );
 		foreach ( $meta['sizes'] as $size => $sizeinfo ) {
-			$intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
+			$intermediate_file = str_replace( $basename_file, $sizeinfo['file'], $file );
 			/** This filter is documented in wp-includes/functions.php */
 			$intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
 			@ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
Index: tests/phpunit/tests/post/attachments.php
===================================================================
--- tests/phpunit/tests/post/attachments.php	(revision 40036)
+++ tests/phpunit/tests/post/attachments.php	(working copy)
@@ -204,13 +204,53 @@
 		// now delete the attachment and make sure all files are gone
 		wp_delete_attachment($id);
 
-		$this->assertFalse( is_file($thumb['path']) );
-		$this->assertFalse( is_file($medium['path']) );
-		$this->assertFalse( is_file($medium_large['path']) );
-		$this->assertFalse( is_file($original) );
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
 	}
 
 	/**
+	 * Test delete of UTF-8 filename.
+	 * @ticket 39039
+	 */
+	function test_insert_image_delete_utf8() {
+		if ( ! function_exists( 'imagejpeg' ) ) {
+			$this->markTestSkipped( 'jpeg support unavailable' );
+		}
+
+		$prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE.
+		$this->assertTrue( false !== $prev_ctype_locale );
+
+		$contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); // Generates thumbnail only.
+		$basename_noext = 'هم اندیشی';
+
+		$upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
+		$this->assertEmpty( $upload['error'] );
+		$basename_noext = sanitize_file_name( $basename_noext );
+
+		$id = $this->_make_attachment( $upload );
+		$uploads = wp_upload_dir();
+
+		// check that the intermediate thumbnail exists.
+		$thumb = image_get_intermediate_size( $id, 'thumbnail' );
+		$this->assertEquals( $basename_noext . '-150x150.jpg', $thumb['file'] );
+		$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
+
+		$meta = wp_get_attachment_metadata( $id );
+		$original = $meta['file'];
+		$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
+
+		// now delete the attachment and make sure all files are gone
+		wp_delete_attachment( $id );
+
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
+		$this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
+
+		setlocale( LC_CTYPE, $prev_ctype_locale );
+	}
+
+	/**
 	 * GUID should never be empty
 	 * @ticket 18310
 	 * @ticket 21963
