Core\FileTransfer\Connection::removeDirectory
Deletes one empty folder on the server. To delete a file, use remove.
The folder must be empty. No call deletes a folder and everything in it, so one wrong path cannot delete a whole tree of files. To delete a full folder, use walk to find everything in it. Delete each file with remove, then each folder, the deepest first.
SFTP sends RMDIR, and FTP and FTPS send RMD.
removeDirectory throws an IOError when the folder is not empty or does not exist, or when the server does not allow the deletion. In each case, nothing is deleted.
The path throws a RuntimeError when it starts with /, or has an empty part or a .. part. A part is the text between two /. A NUL, CR or LF character in the path also throws a RuntimeError. An empty path names the folder of the block itself, and it also throws a RuntimeError. So the folder of the block is never deleted.
Parameters
$pathstringThe path of the folder, relative to the block'sroot.
Return value
Returns void. Nothing.
Errors
RuntimeErrorThe path starts with/, has an empty part or a..part, or has a NUL, CR or LF character.IOErrorThe folder does not exist or is not empty, the path is a file, or the server does not answer.LogicErrorThe connection is closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once. |
Examples
The examples below run with this configuration file.
# The `partner` block reads and writes the folder `partner` beside this file. It needs no server.[file_transfer.partner]directory = "partner"
# The program may open the `partner` block.[capabilities.file_transfer]connect = ["partner"]
# A `directory` block needs read and write access to its folder.[capabilities.fs]read = ["partner"]write = ["partner"]Delete an empty folder
<?nvs// Shows how to delete an empty folder on a server.
var $partner = Core\FileTransfer::open('partner');$partner->makeDirectory('upload-01');
// The folder exists, so this prints "upload-01: yes".echo 'upload-01: ', $partner->exists('upload-01') ? 'yes' : 'no', "\n";
// The folder is empty, so `removeDirectory` deletes it.// The folder does not exist now, so this prints "upload-01: no".$partner->removeDirectory('upload-01');echo 'upload-01: ', $partner->exists('upload-01') ? 'yes' : 'no', "\n";upload-01: yesupload-01: noA folder with files throws
<?nvs// Shows that `removeDirectory` throws an `IOError` when the folder is not empty.
var $partner = Core\FileTransfer::open('partner');$partner->makeDirectory('photos-02');$partner->write('photos-02/beach.jpg', 'image data' as bytes);
// The folder has a file in it, so `removeDirectory` throws.try { $partner->removeDirectory('photos-02'); echo "deleted\n";} catch (IOError $notEmpty) { echo "the folder is not empty\n";}
// Nothing was deleted. This prints "beach.jpg: yes".echo 'beach.jpg: ', $partner->exists('photos-02/beach.jpg') ? 'yes' : 'no', "\n";
$partner->remove('photos-02/beach.jpg');$partner->removeDirectory('photos-02');the folder is not emptybeach.jpg: yesDelete a folder and everything in it
<?nvs// Shows how to delete a folder and everything in it: the files first, then the folders.
var $partner = Core\FileTransfer::open('partner');$partner->makeDirectory('old-03/2025/12');$partner->write('old-03/2025/report.csv', "id\n" as bytes);$partner->write('old-03/2025/12/orders.csv', "id\n" as bytes);
// `walk` gives every file and folder below `old-03`. A folder comes before what is in it.// Each `path()` is tainted: it came from the server, and it must be checked before some uses.array<string> $folders = [];foreach ($partner->walk('old-03') as Core\FileTransfer\Entry $entry) { // `remove` and `removeDirectory` check the path before they send it to the server. // So `Core\Taint::assertTrusted` can safely turn the path into a normal `string` here. string $path = Core\Taint::assertTrusted($entry->path(), 'remove and removeDirectory check the path first'); if ($entry->isDirectory()) { $folders[] = $path; } else { $partner->remove($path); echo 'deleted file ', $path, "\n"; }}
// The loop above deleted every file. `Core\Arr::reverse` puts the deepest folder first.// So each folder is empty when `removeDirectory` deletes it.foreach (Core\Arr::reverse($folders) as string $folder) { $partner->removeDirectory($folder); echo 'deleted folder ', $folder, "\n";}$partner->removeDirectory('old-03');
// This prints "old-03: no".echo 'old-03: ', $partner->exists('old-03') ? 'yes' : 'no', "\n";deleted file old-03/2025/12/orders.csvdeleted file old-03/2025/report.csvdeleted folder old-03/2025/12deleted folder old-03/2025old-03: no