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 |
|
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 | ## Install
|
9 |
|
10 | ```
|
11 | $ npm install del
|
12 | ```
|
13 |
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ```js
|
18 | const 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 |
|
30 | The glob pattern `**` matches all children and *the parent*.
|
31 |
|
32 | So this won't work:
|
33 |
|
34 | ```js
|
35 | del.sync(['public/assets/**', '!public/assets/goat.png']);
|
36 | ```
|
37 |
|
38 | You have to explicitly ignore the parent directories too:
|
39 |
|
40 | ```js
|
41 | del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
|
42 | ```
|
43 |
|
44 | Suggestions on how to improve this welcome!
|
45 |
|
46 |
|
47 | ## API
|
48 |
|
49 | 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()`.
|
50 |
|
51 | ### del(patterns, options?)
|
52 |
|
53 | Returns `Promise<string[]>` with the deleted paths.
|
54 |
|
55 | ### del.sync(patterns, options?)
|
56 |
|
57 | Returns `string[]` with the deleted paths.
|
58 |
|
59 | #### patterns
|
60 |
|
61 | Type: `string | string[]`
|
62 |
|
63 | See 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 |
|
70 | Type: `object`
|
71 |
|
72 | 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.
|
73 |
|
74 | ##### force
|
75 |
|
76 | Type: `boolean`<br>
|
77 | Default: `false`
|
78 |
|
79 | Allow deleting the current working directory and outside.
|
80 |
|
81 | ##### dryRun
|
82 |
|
83 | Type: `boolean`<br>
|
84 | Default: `false`
|
85 |
|
86 | See what would be deleted.
|
87 |
|
88 | ```js
|
89 | const 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 |
|
100 | Type: `number`<br>
|
101 | Default: `Infinity`<br>
|
102 | Minimum: `1`
|
103 |
|
104 | Concurrency limit.
|
105 |
|
106 |
|
107 | ## CLI
|
108 |
|
109 | 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.
|
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>
|