Core\Storage::delete
Core\Storage::delete(string $disk, string $key): voidCore\Storage::delete( string $disk, string $key, ): void
Deletes a file from a disk by its key.
The disk is a [storage.<name>] block in nvs.toml. On a server, the fs.write capability must include the disk's folder. A program you start from the command line needs no capability. When delete returns, the file is gone.
If no file has the key, delete throws an IOError. A missing file usually means a wrong key, and the error shows you that. If a missing file is fine for your program, catch the IOError.
Parameters
$diskstringThe name of a[storage.<name>]block innvs.toml. That block sets the folder the files are saved in. Ataintedvalue is not allowed here, because the program chooses the disk and user input never does.$keystringThe name of the file on the disk. It is 1 to 255 ASCII letters, digits,.,-and_, and it does not start with a.. A/or a\throws an error, so a key never points into another folder.
Return value
Returns void. Nothing. The file is gone when delete returns.
Errors
RuntimeErrorNo[storage.<name>]block with this name sets aroot. Or$keyis not a valid key. Or thefs.readorfs.writecapability does not include the file's path.IOErrorThere is no file with this key, or it could not be deleted. Deleting a missing file throws this error, so a typo in a key is reported.
Examples
The examples below run with this configuration file.
# The `files` disk saves its files in the `disk` folder next to these examples.# The path is relative to the folder of this file.[storage.files]root = "disk"
# The program may read and write files in that folder, and nowhere else.# A path in `[capabilities]` is relative to this file.[capabilities.fs]read = ["disk"]write = ["disk"]Delete a file
<?nvs// Deletes a file from the `files` disk.
Core\Storage::put('files', 'old-report.csv', "id,total\n1,40\n" as bytes);Core\Storage::delete('files', 'old-report.csv');
// The file is gone, so `get` returns `null`. This prints `deleted`.if (Core\Storage::get('files', 'old-report.csv') == null) { echo "deleted\n";}deletedA missing file throws
<?nvs// Shows that deleting a file that does not exist throws an `IOError`.
try { Core\Storage::delete('files', 'never-saved.txt');} catch (IOError $e) { // This prints `nothing to delete`. echo "nothing to delete\n";}nothing to deleteRemove old exports
<?nvs// Deletes every export file on the disk, as a nightly cleanup job does.
Core\Storage::put('files', 'export-monday.csv', "id,total\n" as bytes);Core\Storage::put('files', 'export-tuesday.csv', "id,total\n" as bytes);Core\Storage::put('files', 'invoice-7.pdf', '%PDF' as bytes);
// `list` returns the keys that start with `export-`. The loop deletes each one.foreach (Core\Storage::list('files', { prefix: 'export-' }) as string $key) { Core\Storage::delete('files', $key); echo 'deleted ', $key, "\n";}
// No export file is left. This prints `exports left: 0`.echo 'exports left: ', Core\Arr::count(Core\Storage::list('files', { prefix: 'export-' })), "\n";
// The invoice is still on the disk. This prints `invoice kept`.if (Core\Storage::get('files', 'invoice-7.pdf') != null) { echo "invoice kept\n";}
Core\Storage::delete('files', 'invoice-7.pdf');deleted export-monday.csvdeleted export-tuesday.csvexports left: 0invoice kept