Skip to main content

Deleting files

Removing files and directories is a common task. Deno has a number of functions for this task.

In the case that we want to remove a simple file, we can simply call Deno.remove with the filename as a parameter
await Deno.remove("example.txt");
There is also a sync version of the api available
await Deno.writeTextFile("toBeRemoved.txt", "noop");
Deno.removeSync("toBeRemoved.txt");
If we want to remove a directory, we could do exactly what we did above. If the directory has contents, the call would error out. If we want to recursively delete the contents of a directory, we should set recursive to true
await Deno.remove("./dir", { recursive: true });
A common pattern is to remove a file or directory only if it already exists. The correct way of doing this is by just doing it and trying to catch any NotFound errors.
try {
  await Deno.remove("example.txt");
} catch (err) {
  if (!(err instanceof Deno.errors.NotFound)) {
    throw err;
  }
}

Run this example locally using the Deno CLI:

deno run -W https://docs.deno.com/examples/scripts/deleting_files.ts

Additional resources

Did you find what you needed?

Privacy policy