1 |
3a515b92
|
cagy
|
# del [![Build Status](https://travis-ci.org/sindresorhus/del.svg?branch=master)](https://travis-ci.org/sindresorhus/del) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
|
2 |
|
|
|
3 |
|
|
> Delete files and folders using [globs](https://github.com/isaacs/minimatch#usage)
|
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 |
|
|
---
|
8 |
|
|
|
9 |
|
|
<p align="center">🐶</p>
|
10 |
|
|
<p align="center"><b>Support this project and improve your JavaScript skills with this great <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</b><br>Try his free <a href="https://javascript30.com/friend/AWESOME">JavaScript 30 course</a> for a taste of what to expect. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React</a> and <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> course.</p>
|
11 |
|
|
|
12 |
|
|
---
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
## Install
|
16 |
|
|
|
17 |
|
|
```
|
18 |
|
|
$ npm install del
|
19 |
|
|
```
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
## Usage
|
23 |
|
|
|
24 |
|
|
```js
|
25 |
|
|
const del = require('del');
|
26 |
|
|
|
27 |
|
|
(async () => {
|
28 |
|
|
const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']);
|
29 |
|
|
|
30 |
|
|
console.log('Deleted files and folders:\n', deletedPaths.join('\n'));
|
31 |
|
|
})();
|
32 |
|
|
```
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
## Beware
|
36 |
|
|
|
37 |
|
|
The glob pattern `**` matches all children and *the parent*.
|
38 |
|
|
|
39 |
|
|
So this won't work:
|
40 |
|
|
|
41 |
|
|
```js
|
42 |
|
|
del.sync(['public/assets/**', '!public/assets/goat.png']);
|
43 |
|
|
```
|
44 |
|
|
|
45 |
|
|
You have to explicitly ignore the parent directories too:
|
46 |
|
|
|
47 |
|
|
```js
|
48 |
|
|
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
|
49 |
|
|
```
|
50 |
|
|
|
51 |
|
|
Suggestions on how to improve this welcome!
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
## API
|
55 |
|
|
|
56 |
|
|
### del(patterns, [options])
|
57 |
|
|
|
58 |
|
|
Returns a promise for an array of deleted paths.
|
59 |
|
|
|
60 |
|
|
### del.sync(patterns, [options])
|
61 |
|
|
|
62 |
|
|
Returns an array of deleted paths.
|
63 |
|
|
|
64 |
|
|
#### patterns
|
65 |
|
|
|
66 |
|
|
Type: `string` `string[]`
|
67 |
|
|
|
68 |
|
|
See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage).
|
69 |
|
|
|
70 |
|
|
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
|
71 |
|
|
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
|
72 |
|
|
|
73 |
|
|
#### options
|
74 |
|
|
|
75 |
|
|
Type: `Object`
|
76 |
|
|
|
77 |
|
|
See the [`glob` options](https://github.com/isaacs/node-glob#options).
|
78 |
|
|
|
79 |
|
|
##### force
|
80 |
|
|
|
81 |
|
|
Type: `boolean`<br>
|
82 |
|
|
Default: `false`
|
83 |
|
|
|
84 |
|
|
Allow deleting the current working directory and outside.
|
85 |
|
|
|
86 |
|
|
##### dryRun
|
87 |
|
|
|
88 |
|
|
Type: `boolean`<br>
|
89 |
|
|
Default: `false`
|
90 |
|
|
|
91 |
|
|
See what would be deleted.
|
92 |
|
|
|
93 |
|
|
```js
|
94 |
|
|
const del = require('del');
|
95 |
|
|
|
96 |
|
|
(async () => {
|
97 |
|
|
const deletedPaths = await del(['tmp/*.js'], {dryRun: true});
|
98 |
|
|
|
99 |
|
|
console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n'));
|
100 |
|
|
})();
|
101 |
|
|
```
|
102 |
|
|
|
103 |
|
|
##### concurrency
|
104 |
|
|
|
105 |
|
|
Type: `number`<br>
|
106 |
|
|
Default: `Infinity`<br>
|
107 |
|
|
Minimum: `1`
|
108 |
|
|
|
109 |
|
|
Concurrency limit.
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
## CLI
|
113 |
|
|
|
114 |
|
|
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.
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
## Related
|
118 |
|
|
|
119 |
|
|
- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
|
120 |
|
|
- [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
## License
|
124 |
|
|
|
125 |
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|