$files->rmdir() method

Remove a directory and optionally everything within it (recursively)

Unlike PHP's rmdir() function, this method provides a recursive option, which can be enabled by specifying true for the $recursive argument. You should be careful with this option, as it can easily wipe out an entire directory tree in a flash.

Note that the $options argument was added in 3.0.118.

Example

// Remove directory /site/assets/cache/foo-bar/ and everything in it
$files->rmdir($config->paths->cache . 'foo-bar/', true);

// Remove directory after ensuring $pathname is somewhere within /site/assets/
$files->rmdir($pathname, true, [ 'limitPath' => $config->paths->assets ]); 

Usage

// basic usage
$bool = $files->rmdir(string $path);

// usage with all arguments
$bool = $files->rmdir(string $path, bool $recursive = false, $options = []);

Arguments

NameType(s)Description
pathstring

Path/directory you want to remove

recursive (optional)bool

If set to true, all files and directories in $path will be recursively removed as well (default=false).

options (optional)array, bool, string

Optional settings to adjust behavior or (bool|string) for limitPath option:

  • limitPath (string|bool|array): Must be somewhere within given path, boolean true for site assets, or false to disable (default=false).
  • throw (bool): Throw verbose WireException (rather than return false) when potentially consequential fail (default=false).

Return value

bool

True on success, false on failure


$files methods and properties

API reference based on ProcessWire core version 3.0.228

Latest news

  • ProcessWire Weekly #489
    In the 489th issue of ProcessWire Weekly we'll check out what's new in the core this week, share a brand-new recipe of the week, and more. Read on!
    Weekly.pw / 23 September 2023
  • ProcessWire 3.0.226 new main/master version
    After 8 months in development we are excited to bring you ProcessWire 3.0.226 main/master. This version has a ton of great new features, improvements and optimizations, plus more than 100 issue fixes. This post takes an in-depth look at highlights from this great new version.
    Blog / 25 August 2023
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK