Skip to content

Core\Storage::delete

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 in nvs.toml. That block sets the folder the files are saved in. A tainted value 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 a root. Or $key is not a valid key. Or the fs.read or fs.write capability 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.

nvs.toml
# 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";
}
Output
deleted

A 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";
}
Output
nothing to delete

Remove 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');
Output
deleted export-monday.csv
deleted export-tuesday.csv
exports left: 0
invoice kept