Skip to content

Core\IO::remove

Deletes one file.

Core\IO::remove takes the path of a file and deletes it. After the call, the file does not exist and Core\IO::exists returns false for that path.

remove deletes files only. To delete an empty folder, use Core\IO::removeDirectory. There is no way to delete a folder and everything in it with one call. A program that wants this lists the folder and deletes each file first.

remove throws an IOError when there is no file at the path, when the path is a folder, or when the operating system does not allow the deletion. So a program always knows if a file was really deleted.

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

Parameters

  • $pathstringThe file to delete. To delete a directory, use removeDirectory.

Return value

Returns void. Nothing. Removing a path where nothing exists throws an error. A program that deleted nothing is never told that it succeeded.

Errors

  • RuntimeErrorThe configuration does not grant fs.write for this path. Permission to read a folder does not give permission to delete from it.
  • IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the path, it is a directory, or the process may not delete it.

Examples

Delete a file

<?nvs
// Shows how to delete a file with `Core\IO::remove`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'report.txt');
Core\IO::write($path, "Sales: 1200\n");
// The file exists now, so this prints "before: true".
echo 'before: ', Core\IO::exists($path) ? 'true' : 'false', "\n";
Core\IO::remove($path);
// The file is deleted, so this prints "after: false".
echo 'after: ', Core\IO::exists($path) ? 'true' : 'false', "\n";
Output
before: true
after: false

A missing file throws

<?nvs
// Shows that `Core\IO::remove` throws an `IOError` when there is no file to delete.
string $dir = Core\IO::createTemporaryDirectory();
string $path = Core\Path::join($dir, 'cache.txt');
Core\IO::write($path, "old data\n");
// The first call deletes the file. The second call finds nothing to delete,
// so it throws an `IOError`. This prints "deleted" and then "already gone".
try {
Core\IO::remove($path);
echo 'deleted', "\n";
Core\IO::remove($path);
echo 'deleted again', "\n";
} catch (IOError $e) {
echo 'already gone', "\n";
}
// `remove` deletes files only. A folder is not a file, so this prints "not a file".
try {
Core\IO::remove($dir);
} catch (IOError $e) {
echo 'not a file', "\n";
}
Output
deleted
already gone
not a file

Clean up unfinished downloads

<?nvs
// Shows how a program deletes the unfinished downloads in a folder and keeps the finished files.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'downloads');
Core\IO::makeDirectory($dir);
Core\IO::write(Core\Path::join($dir, 'invoice-1.pdf'), 'finished');
Core\IO::write(Core\Path::join($dir, 'invoice-2.pdf.part'), 'half');
Core\IO::write(Core\Path::join($dir, 'photo.jpg.part'), 'half');
// An unfinished download has a name that ends with ".part". Each of these files is deleted.
foreach (Core\IO::list($dir) as string $name) {
if (Core\Str::endsWith($name, '.part')) {
Core\IO::remove(Core\Path::join($dir, $name));
}
}
// Only the finished file is left. This prints "invoice-1.pdf".
foreach (Core\IO::list($dir) as string $name) {
echo $name, "\n";
}
Output
invoice-1.pdf