Deno - Delete File & Directory Examples

This tutorial shows you how to remove a file or a directory in Deno.

If you need to delete a file or a directory in a Deno application, there are built-in functions that can be used. Below are the examples of how to use the functions

Delete File/Directory

For deleting a file or a directory using any of the functions below, you need to add --allow-write flag.

Using Deno.remove

Deno.removeSync can be used to remove a file or a directory.

  function Deno.remove(path: string | URL, options?: RemoveOptions): Promise<void>

To delete a file or a directory, you need to pass the path to the file or directory as the first argument. You can use either absolute or relative path. If you use a relative path, the passed path must be relative to the working directory, not relative to the file. The working directory defaults to the directory where deno run command is run. To change the working directory, use Deno.chdir command.

Below is the usage example.

  await Deno.remove('hello.txt');

It can also be used to delete a directory. However, it will throw an error if the directory is not empty.

  error: Uncaught (in promise) Error: Directory not empty (os error 39)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.remove (deno:cli/rt/30_fs.js:160:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_delete.ts:1:1

If you want to delete a non-empty directory, you need to pass the RemoveOptions argument which is an object with the following field.

  • boolean recursive: Whether to allow removal a non-empty directory. Defaults to false.
  await Deno.remove('files', { recursive: true });

Using Deno.removeSync

Deno.removeSync is similar to Deno.remove, but it performs the operation synchronously.

  function Deno.remove(path: string | URL, options?: RemoveOptions): Promise<void>

Below is the usage example.

  Deno.removeSync('hello.txt');

Error Handling

There can be an error while trying to delete a file. Besides the above error, you may also get an error if the file or directory to be deleted doesn't exist.

  error: Uncaught (in promise) NotFound: No such file or directory (os error 2)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.remove (deno:cli/rt/30_fs.js:160:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_delete.ts:1:1

Another cause of error is permission denied.

  Check file:///home/ivan/Projects/deno/src/examples/file_delete.ts
  error: Uncaught (in promise) PermissionDenied: Permission denied (os error 13)
      at processResponse (core.js:223:11)
      at Object.jsonOpAsync (core.js:240:12)
      at async Object.remove (deno:cli/rt/30_fs.js:160:5)
      at async file:///home/ivan/Projects/deno/src/examples/file_delete.ts:1:1

To handle an error, what you need to do is wrap the code inside a try-catch block.

  try {
    Deno.removeSync('hello.txt', 'hello_copy.txt');
  } catch (err) {
    console.error(err);
  }

 

Related Posts: