Skip to content

Core\IO::removeDirectory

Core\IO::removeDirectory(string $path): voidCore\IO::removeDirectory(
    string $path,
): void

Deletes one empty folder.

Core\IO::removeDirectory takes the path of a folder and deletes it. The folder must be empty. After the call, the folder does not exist and Core\IO::isDirectory returns false for that path.

There is no way to delete a folder and everything in it with one call. With such a call, one wrong path could delete a whole tree of files. To delete a full folder, list it first. Delete each file with Core\IO::remove and each inner folder the same way. Then delete the folder itself.

removeDirectory throws an IOError when the folder still has files or folders in it. It throws the same error when nothing is at the path, or when the path is a file. It also throws an IOError when the operating system does not allow the deletion. In each case, nothing is deleted.

A program you start from the command line may delete any folder. On a server, a program may delete folders only inside the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability. Without it, removeDirectory throws a RuntimeError.

Parameters

  • $pathstringThe directory to delete. It must be empty.

Return value

Returns void. Nothing. A failure throws an error. It never returns false.

Errors

  • RuntimeErrorThe configuration does not grant fs.write for this path.
  • IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the path, it is not a directory, or it still has entries in it.

Changelog

VersionChange
0.0.6Core\IO writes the word "directory" in full: createTemporaryDirectory, makeDirectory, removeDirectory and isDirectory. Core\IO::metadata returns the Core\IO\Metadata of a file. The old names were temporaryDir, makeDir, removeDir, isDir and stat.

Examples

Delete an empty folder

<?nvs
// Shows how to delete an empty folder with `Core\IO::removeDirectory`.
string $folder = Core\Path::join(Core\IO::createTemporaryDirectory(), 'uploads');
Core\IO::makeDirectory($folder);
// The folder exists now, so this prints "before: true".
echo 'before: ', Core\IO::isDirectory($folder) ? 'true' : 'false', "\n";
Core\IO::removeDirectory($folder);
// The folder is deleted, so this prints "after: false".
echo 'after: ', Core\IO::isDirectory($folder) ? 'true' : 'false', "\n";
Output
before: true
after: false

A folder with files throws

<?nvs
// Shows that `Core\IO::removeDirectory` throws an `IOError` when the folder is not empty.
string $folder = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');
Core\IO::makeDirectory($folder);
Core\IO::write(Core\Path::join($folder, 'beach.jpg'), 'image data');
// The folder has a file in it, so `removeDirectory` throws. This prints "the folder is not empty".
try {
Core\IO::removeDirectory($folder);
echo 'deleted', "\n";
} catch (IOError $e) {
echo 'the folder is not empty', "\n";
}
// Nothing was deleted. This prints "beach.jpg is still here".
if (Core\IO::exists(Core\Path::join($folder, 'beach.jpg'))) {
echo 'beach.jpg is still here', "\n";
}
Output
the folder is not empty
beach.jpg is still here

Delete a folder and everything in it

<?nvs
// Shows how a program deletes a folder with all its files and folders, one entry at a time.
class Folder
{
// This deletes every file in the folder, and every inner folder the same way.
// Then the folder is empty, and `removeDirectory` deletes it.
public static function deleteAll(string $folder): void
{
foreach (Core\IO::list($folder) as string $name) {
string $path = Core\Path::join($folder, $name);
if (Core\IO::isDirectory($path)) {
Folder::deleteAll($path);
} else {
Core\IO::remove($path);
}
}
Core\IO::removeDirectory($folder);
}
}
string $build = Core\Path::join(Core\IO::createTemporaryDirectory(), 'build');
Core\IO::makeDirectory(Core\Path::join($build, 'css'));
Core\IO::makeDirectory(Core\Path::join($build, 'js', 'vendor'));
Core\IO::write(Core\Path::join($build, 'index.html'), '<p>Hello</p>');
Core\IO::write(Core\Path::join($build, 'css', 'site.css'), 'p { color: red; }');
Core\IO::write(Core\Path::join($build, 'js', 'vendor', 'lib.js'), '// library');
Folder::deleteAll($build);
// The whole build folder is deleted, so this prints "build folder: false".
echo 'build folder: ', Core\IO::exists($build) ? 'true' : 'false', "\n";
Output
build folder: false