UNPKG

4.13 kBMarkdownView Raw
1# del
2
3> Delete files and directories using [globs](https://github.com/sindresorhus/globby#globbing-patterns)
4
5Similar 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
10npm install del
11```
12
13## Usage
14
15```js
16import {deleteAsync} from 'del';
17
18const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
19const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);
20
21console.log('Deleted files:\n', deletedFilePaths.join('\n'));
22console.log('\n\n');
23console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
24```
25
26## Beware
27
28The glob pattern `**` matches all children and *the parent*.
29
30So this won't work:
31
32```js
33deleteSync(['public/assets/**', '!public/assets/goat.png']);
34```
35
36You have to explicitly ignore the parent directories too:
37
38```js
39deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
40```
41
42To delete all subdirectories inside `public/`, you can do:
43
44```js
45deleteSync(['public/*/']);
46```
47
48Suggestions on how to improve this welcome!
49
50## API
51
52Note 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
56Returns `Promise<string[]>` with the deleted paths.
57
58### deleteSync(patterns, options?)
59
60Returns `string[]` with the deleted paths.
61
62#### patterns
63
64Type: `string | string[]`
65
66See 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
73Type: `object`
74
75You 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
79Type: `boolean`\
80Default: `false`
81
82Allow deleting the current working directory and outside.
83
84##### dryRun
85
86Type: `boolean`\
87Default: `false`
88
89See what would be deleted.
90
91```js
92import {deleteAsync} from 'del';
93
94const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
95
96console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
97```
98
99##### dot
100
101Type: `boolean`\
102Default: `false`
103
104Allow patterns to match files/folders that start with a period (`.`).
105
106This option is passed through to [`fast-glob`](https://github.com/mrmlnc/fast-glob#dot).
107
108Note that an explicit dot in a portion of the pattern will always match dot files.
109
110**Example**
111
112```
113directory/
114├── .editorconfig
115└── package.json
116```
117
118```js
119import {deleteSync} from 'del';
120
121deleteSync('*', {dot: false});
122//=> ['package.json']
123deleteSync('*', {dot: true});
124//=> ['.editorconfig', 'package.json']
125```
126
127##### concurrency
128
129Type: `number`\
130Default: `Infinity`\
131Minimum: `1`
132
133Concurrency limit.
134
135##### onProgress
136
137Type: `(progress: ProgressData) => void`
138
139Called after each file or directory is deleted.
140
141```js
142import {deleteAsync} from 'del';
143
144await 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
166See [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