-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathdeleting_files.ts
36 lines (32 loc) · 1.08 KB
/
deleting_files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @title Deleting files
* @difficulty beginner
* @tags cli
* @resource {https://docs.deno.com/api/deno/~/Deno.remove} Doc: Deno.remove
* @group File System
*
* 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
Deno.removeSync("example.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;
}
}