WP_Filesystem_Direct::delete( string $file, bool $recursive = false, string|false $type = false ): bool
- Since
- 2.5.0
- Source
wp-admin/includes/class-wp-filesystem-direct.php:392
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- Path to the file or directory.
$recursivebooloptional- If set to true, deletes files and folders recursively.
Default false.Default:false $typestring|falseoptional- Type of resource. 'f' for file, 'd' for directory.
Default false.Default:false
Return value
bool- True on success, false on failure.
Performance profile
How much work a call to WP_Filesystem_Direct::delete() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 6–46
- Plugin surface
- None
- Called by
- 3
Reads or writes the filesystem via unlink().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 70.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- sqldatabase query
WP_Filesystem_Direct::delete()this function does it - filesystemfilesystem access
unlink()called directly
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Filesystem_Direct::delete() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($file) | 6 | none |
!empty($file) && $type === "f" | 16 | unlink() |
!empty($file) && $type !== "f" && ->is_file() | 20 | ->is_file(), unlink() |
!empty($file) && $type !== "f" && !->is_file() && ->is_dir() | 25 | ->is_file(), ->is_dir(), rmdir() |
!empty($file) && $type !== "f" && !->is_file() && !file_exists() | 32–35 | ->is_file(), trailingslashit(), ->dirlist(), file_exists() |
!empty($file) && $type !== "f" && !->is_file() && !->is_dir() && !file_exists() | 36–39 | ->is_file(), ->is_dir(), trailingslashit(), ->dirlist(), file_exists() |
!empty($file) && $type !== "f" && !->is_file() && file_exists() | 38–42 | ->is_file(), trailingslashit(), ->dirlist(), file_exists(), rmdir() |
!empty($file) && $type !== "f" && !->is_file() && !->is_dir() && file_exists() | 42–46 | ->is_file(), ->is_dir(), trailingslashit(), ->dirlist(), file_exists(), rmdir() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 70 | 6–46 | 11 | |
| 8.5 | 70 | 6–46 | 11 | |
| 8.4 | 70 | 6–46 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 73 | 6–49 | 11 | |
| 8.2 | 73 | 6–49 | 11 | |
| 8.1 | 73 | 6–49 | 11 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 74 | 6–49 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- trailingslashit()Appends a trailing slash.
- WP_Filesystem_Direct::is_file()Checks if resource is a file.
- WP_Filesystem_Direct::is_dir()Checks if resource is a directory.
- WP_Filesystem_Direct::dirlist()Gets details for files in a directory or a specific file.
- WP_Filesystem_Direct::delete()Deletes a file or directory.
Used by · 3
- WP_Filesystem_Direct::delete()Deletes a file or directory.
- WP_Filesystem_Direct::move()Moves a file or directory.
- WP_Filesystem_Direct::rmdir()Deletes a directory.
Source code
public function delete( $file, $recursive = false, $type = false ) { if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. return false; } $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise. if ( 'f' === $type || $this->is_file( $file ) ) { return @unlink( $file ); } if ( ! $recursive && $this->is_dir( $file ) ) { return @rmdir( $file ); } // At this point it's a folder, and we're in recursive mode. $file = trailingslashit( $file ); $filelist = $this->dirlist( $file, true ); $retval = true; if ( is_array( $filelist ) ) { foreach ( $filelist as $filename => $fileinfo ) { if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { $retval = false; } } } if ( file_exists( $file ) && ! @rmdir( $file ) ) { $retval = false; } return $retval; }Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-admin/includes/class-wp-filesystem-direct.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.