UNPKG

3.52 kBMarkdownView Raw
1# 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 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
8## Install
9
10```
11$ npm install del
12```
13
14
15## Usage
16
17```js
18const del = require('del');
19
20(async () => {
21 const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']);
22
23 console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
24})();
25```
26
27
28## Beware
29
30The glob pattern `**` matches all children and *the parent*.
31
32So this won't work:
33
34```js
35del.sync(['public/assets/**', '!public/assets/goat.png']);
36```
37
38You have to explicitly ignore the parent directories too:
39
40```js
41del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
42```
43
44Suggestions on how to improve this welcome!
45
46
47## API
48
49Note 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()`.
50
51### del(patterns, options?)
52
53Returns `Promise<string[]>` with the deleted paths.
54
55### del.sync(patterns, options?)
56
57Returns `string[]` with the deleted paths.
58
59#### patterns
60
61Type: `string | string[]`
62
63See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
64
65- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
66- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
67
68#### options
69
70Type: `object`
71
72You 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.
73
74##### force
75
76Type: `boolean`<br>
77Default: `false`
78
79Allow deleting the current working directory and outside.
80
81##### dryRun
82
83Type: `boolean`<br>
84Default: `false`
85
86See what would be deleted.
87
88```js
89const del = require('del');
90
91(async () => {
92 const deletedPaths = await del(['temp/*.js'], {dryRun: true});
93
94 console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
95})();
96```
97
98##### concurrency
99
100Type: `number`<br>
101Default: `Infinity`<br>
102Minimum: `1`
103
104Concurrency limit.
105
106
107## CLI
108
109See [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.
110
111
112## Related
113
114- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
115- [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching
116
117
118---
119
120<div align="center">
121 <b>
122 <a href="https://tidelift.com/subscription/pkg/npm-del?utm_source=npm-del&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
123 </b>
124 <br>
125 <sub>
126 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
127 </sub>
128</div>