Title: wp_delete_file_from_directory
Published: October 5, 2018
Last modified: May 20, 2026

---

# wp_delete_file_from_directory( string $file, string $directory ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#wp--skip-link--target)

Deletes a file if its path is within the given directory.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#parameters)󠁿

 `$file`stringrequired

Absolute path to the file to delete.

`$directory`stringrequired

Absolute path to a directory.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#return)󠁿

 bool True on success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#source)󠁿

    ```php
    function wp_delete_file_from_directory( $file, $directory ) {
    	if ( wp_is_stream( $file ) ) {
    		$real_file      = $file;
    		$real_directory = $directory;
    	} else {
    		$real_file      = realpath( wp_normalize_path( $file ) );
    		$real_directory = realpath( wp_normalize_path( $directory ) );
    	}

    	if ( false !== $real_file ) {
    		$real_file = wp_normalize_path( $real_file );
    	}

    	if ( false !== $real_directory ) {
    		$real_directory = wp_normalize_path( $real_directory );
    	}

    	if ( false === $real_file || false === $real_directory || ! str_starts_with( $real_file, trailingslashit( $real_directory ) ) ) {
    		return false;
    	}

    	return wp_delete_file( $file );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/functions.php#L7786)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/functions.php#L7786-L7808)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_delete_file()](https://developer.wordpress.org/reference/functions/wp_delete_file/)`wp-includes/functions.php` |

Deletes a file.

  | 
| [wp_is_stream()](https://developer.wordpress.org/reference/functions/wp_is_stream/)`wp-includes/functions.php` |

Tests if a given path is a stream URL

  | 
| [wp_normalize_path()](https://developer.wordpress.org/reference/functions/wp_normalize_path/)`wp-includes/functions.php` |

Normalizes a filesystem path.

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#)

| Used by | Description | 
| [wp_delete_attachment_files()](https://developer.wordpress.org/reference/functions/wp_delete_attachment_files/)`wp-includes/post.php` |

Deletes all files that belong to the given attachment.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_delete_file_from_directory/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.9.7](https://developer.wordpress.org/reference/since/4.9.7/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_delete_file_from_directory%2F)
before being able to contribute a note or feedback.