1 | # del
|
2 |
|
3 | > Delete files and directories using [globs](https://github.com/sindresorhus/globby#globbing-patterns)
|
4 |
|
5 | Similar to [rimraf](https://github.com/isaacs/rimraf), but with a Promise API and support for multiple files and globbing. It also protects you against deleting the current working directory and above.
|
6 |
|
7 | ## Install
|
8 |
|
9 | ```sh
|
10 | npm install del
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | import {deleteAsync} from 'del';
|
17 |
|
18 | const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
|
19 | const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);
|
20 |
|
21 | console.log('Deleted files:\n', deletedFilePaths.join('\n'));
|
22 | console.log('\n\n');
|
23 | console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
|
24 | ```
|
25 |
|
26 | ## Beware
|
27 |
|
28 | The glob pattern `**` matches all children and *the parent*.
|
29 |
|
30 | So this won't work:
|
31 |
|
32 | ```js
|
33 | deleteSync(['public/assets/**', '!public/assets/goat.png']);
|
34 | ```
|
35 |
|
36 | You have to explicitly ignore the parent directories too:
|
37 |
|
38 | ```js
|
39 | deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
|
40 | ```
|
41 |
|
42 | To delete all subdirectories inside `public/`, you can do:
|
43 |
|
44 | ```js
|
45 | deleteSync(['public/*/']);
|
46 | ```
|
47 |
|
48 | Suggestions on how to improve this welcome!
|
49 |
|
50 | ## API
|
51 |
|
52 | Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`.
|
53 |
|
54 | ### deleteAsync(patterns, options?)
|
55 |
|
56 | Returns `Promise<string[]>` with the deleted paths.
|
57 |
|
58 | ### deleteSync(patterns, options?)
|
59 |
|
60 | Returns `string[]` with the deleted paths.
|
61 |
|
62 | #### patterns
|
63 |
|
64 | Type: `string | string[]`
|
65 |
|
66 | See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
|
67 |
|
68 | - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
|
69 | - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
|
70 |
|
71 | #### options
|
72 |
|
73 | Type: `object`
|
74 |
|
75 | You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the below options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
|
76 |
|
77 | ##### force
|
78 |
|
79 | Type: `boolean`\
|
80 | Default: `false`
|
81 |
|
82 | Allow deleting the current working directory and outside.
|
83 |
|
84 | ##### dryRun
|
85 |
|
86 | Type: `boolean`\
|
87 | Default: `false`
|
88 |
|
89 | See what would be deleted.
|
90 |
|
91 | ```js
|
92 | import {deleteAsync} from 'del';
|
93 |
|
94 | const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
|
95 |
|
96 | console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
|
97 | ```
|
98 |
|
99 | ##### dot
|
100 |
|
101 | Type: `boolean`\
|
102 | Default: `false`
|
103 |
|
104 | Allow patterns to match files/folders that start with a period (`.`).
|
105 |
|
106 | This option is passed through to [`fast-glob`](https://github.com/mrmlnc/fast-glob#dot).
|
107 |
|
108 | Note that an explicit dot in a portion of the pattern will always match dot files.
|
109 |
|
110 | **Example**
|
111 |
|
112 | ```
|
113 | directory/
|
114 | ├── .editorconfig
|
115 | └── package.json
|
116 | ```
|
117 |
|
118 | ```js
|
119 | import {deleteSync} from 'del';
|
120 |
|
121 | deleteSync('*', {dot: false});
|
122 | //=> ['package.json']
|
123 | deleteSync('*', {dot: true});
|
124 | //=> ['.editorconfig', 'package.json']
|
125 | ```
|
126 |
|
127 | ##### concurrency
|
128 |
|
129 | Type: `number`\
|
130 | Default: `Infinity`\
|
131 | Minimum: `1`
|
132 |
|
133 | Concurrency limit.
|
134 |
|
135 | ##### onProgress
|
136 |
|
137 | Type: `(progress: ProgressData) => void`
|
138 |
|
139 | Called after each file or directory is deleted.
|
140 |
|
141 | ```js
|
142 | import {deleteAsync} from 'del';
|
143 |
|
144 | await deleteAsync(patterns, {
|
145 | onProgress: progress => {
|
146 | // …
|
147 | }});
|
148 | ```
|
149 |
|
150 | ###### ProgressData
|
151 |
|
152 | ```js
|
153 | {
|
154 | totalCount: number,
|
155 | deletedCount: number,
|
156 | percent: number,
|
157 | path?: string
|
158 | }
|
159 | ```
|
160 |
|
161 | - `percent` is a value between `0` and `1`
|
162 | - `path` is the absolute path of the deleted file or directory. It will not be present if nothing was deleted.
|
163 |
|
164 | ## CLI
|
165 |
|
166 | See [del-cli](https://github.com/sindresorhus/del-cli) for a CLI for this module and [trash-cli](https://github.com/sindresorhus/trash-cli) for a safe version that is suitable for running by hand.
|
167 |
|
168 | ## Related
|
169 |
|
170 | - [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
|
171 | - [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching
|