1 | The [UNIX command](<http://en.wikipedia.org/wiki/Rm_(Unix)>) `rm -rf` for node.
|
2 |
|
3 | Install with `npm install rimraf`.
|
4 |
|
5 | ## Major Changes from v3 to v4
|
6 |
|
7 | - The function returns a `Promise` instead of taking a callback.
|
8 | - Built-in glob support removed.
|
9 | - Functions take arrays of paths, as well as a single path.
|
10 | - Native implementation used by default when available, except on
|
11 | Windows, where this implementation is faster and more reliable.
|
12 | - New implementation on Windows, falling back to "move then
|
13 | remove" strategy when exponential backoff for `EBUSY` fails to
|
14 | resolve the situation.
|
15 | - Simplified implementation on Posix, since the Windows
|
16 | affordances are not necessary there.
|
17 |
|
18 | ## API
|
19 |
|
20 | Hybrid module, load either with `import` or `require()`.
|
21 |
|
22 | ```js
|
23 | // default export is the main rimraf function
|
24 | import rimraf from 'rimraf'
|
25 | // or
|
26 | const rimraf = require('rimraf').default
|
27 |
|
28 | // other strategies exported as well
|
29 | import { rimraf, rimrafSync, native, nativeSync } from 'rimraf'
|
30 | // or
|
31 | const { rimraf, rimrafSync, native, nativeSync } = require('rimraf')
|
32 | ```
|
33 |
|
34 | ### `rimraf(f, [opts]) -> Promise`
|
35 |
|
36 | This first parameter is a path or array of paths. The second
|
37 | argument is an options object.
|
38 |
|
39 | Options:
|
40 |
|
41 | - `preserveRoot`: If set to boolean `false`, then allow the
|
42 | recursive removal of the root directory. Otherwise, this is
|
43 | not allowed.
|
44 | - `tmp`: Windows only. Temp folder to use to place files and
|
45 | folders for the "move then remove" fallback. Must be on the
|
46 | same physical device as the path being deleted. Defaults to
|
47 | `os.tmpdir()` when that is on the same drive letter as the path
|
48 | being deleted, or `${drive}:\temp` if present, or `${drive}:\`
|
49 | if not.
|
50 | - `maxRetries`: Windows and Native only. Maximum number of
|
51 | retry attempts in case of `EBUSY`, `EMFILE`, and `ENFILE`
|
52 | errors. Default `10` for Windows implementation, `0` for Native
|
53 | implementation.
|
54 | - `backoff`: Windows only. Rate of exponential backoff for async
|
55 | removal in case of `EBUSY`, `EMFILE`, and `ENFILE` errors.
|
56 | Should be a number greater than 1. Default `1.2`
|
57 | - `maxBackoff`: Windows only. Maximum total backoff time in ms to
|
58 | attempt asynchronous retries in case of `EBUSY`, `EMFILE`, and
|
59 | `ENFILE` errors. Default `200`. With the default `1.2` backoff
|
60 | rate, this results in 14 retries, with the final retry being
|
61 | delayed 33ms.
|
62 | - `retryDelay`: Native only. Time to wait between retries, using
|
63 | linear backoff. Default `100`.
|
64 |
|
65 | Any other options are provided to the native Node.js `fs.rm` implementation
|
66 | when that is used.
|
67 |
|
68 | This will attempt to choose the best implementation, based on Node.js
|
69 | version and `process.platform`. To force a specific implementation, use
|
70 | one of the other functions provided.
|
71 |
|
72 | ### `rimraf.sync(f, [opts])` `rimraf.rimrafSync(f, [opts])`
|
73 |
|
74 | Synchronous form of `rimraf()`
|
75 |
|
76 | Note that, unlike many file system operations, the synchronous form will
|
77 | typically be significantly _slower_ than the async form, because recursive
|
78 | deletion is extremely parallelizable.
|
79 |
|
80 | ### `rimraf.native(f, [opts])`
|
81 |
|
82 | Uses the built-in `fs.rm` implementation that Node.js provides. This is
|
83 | used by default on Node.js versions greater than or equal to `14.14.0`.
|
84 |
|
85 | ### `rimraf.nativeSync(f, [opts])` `rimraf.native.sync(f, [opts])`
|
86 |
|
87 | Synchronous form of `rimraf.native`
|
88 |
|
89 | ### `rimraf.manual(f, [opts])`
|
90 |
|
91 | Use the JavaScript implementation appropriate for your operating system.
|
92 |
|
93 | ### `rimraf.manualSync(f, [opts])` `rimraf.manualSync(f, opts)`
|
94 |
|
95 | Synchronous form of `rimraf.manual()`
|
96 |
|
97 | ### `rimraf.windows(f, [opts])`
|
98 |
|
99 | JavaScript implementation of file removal appropriate for Windows
|
100 | platforms. Works around `unlink` and `rmdir` not being atomic
|
101 | operations, and `EPERM` when deleting files with certain
|
102 | permission modes.
|
103 |
|
104 | First deletes all non-directory files within the tree, and then
|
105 | removes all directories, which should ideally be empty by that
|
106 | time. When an `ENOTEMPTY` is raised in the second pass, falls
|
107 | back to the `rimraf.moveRemove` strategy as needed.
|
108 |
|
109 | ### `rimraf.windows.sync(path, [opts])` `rimraf.windowsSync(path, [opts])`
|
110 |
|
111 | Synchronous form of `rimraf.windows()`
|
112 |
|
113 | ### `rimraf.moveRemove(path, [opts])`
|
114 |
|
115 | Moves all files and folders to the parent directory of `path`
|
116 | with a temporary filename prior to attempting to remove them.
|
117 |
|
118 | Note that, in cases where the operation fails, this _may_ leave
|
119 | files lying around in the parent directory with names like
|
120 | `.file-basename.txt.0.123412341`. Until the Windows kernel
|
121 | provides a way to perform atomic `unlink` and `rmdir` operations,
|
122 | this is unfortunately unavoidable.
|
123 |
|
124 | To move files to a different temporary directory other than the
|
125 | parent, provide `opts.tmp`. Note that this _must_ be on the same
|
126 | physical device as the folder being deleted, or else the
|
127 | operation will fail.
|
128 |
|
129 | This is the slowest strategy, but most reliable on Windows
|
130 | platforms. Used as a last-ditch fallback by `rimraf.windows()`.
|
131 |
|
132 | ### `rimraf.moveRemove.sync(path, [opts])` `rimraf.moveRemoveSync(path, [opts])`
|
133 |
|
134 | Synchronous form of `rimraf.moveRemove()`
|
135 |
|
136 | ### Command Line Interface
|
137 |
|
138 | ```
|
139 | rimraf version 4.0.4
|
140 |
|
141 | Usage: rimraf <path> [<path> ...]
|
142 | Deletes all files and folders at "path", recursively.
|
143 |
|
144 | Options:
|
145 | -- Treat all subsequent arguments as paths
|
146 | -h --help Display this usage info
|
147 | --preserve-root Do not remove '/' recursively (default)
|
148 | --no-preserve-root Do not treat '/' specially
|
149 |
|
150 | --impl=<type> Specify the implementationt to use.
|
151 | rimraf: choose the best option
|
152 | native: the built-in implementation in Node.js
|
153 | manual: the platform-specific JS implementation
|
154 | posix: the Posix JS implementation
|
155 | windows: the Windows JS implementation
|
156 | move-remove: a slower Windows JS fallback implementation
|
157 |
|
158 | Implementation-specific options:
|
159 | --tmp=<path> Folder to hold temp files for 'move-remove' implementation
|
160 | --max-retries=<n> maxRetries for the 'native' and 'windows' implementations
|
161 | --retry-delay=<n> retryDelay for the 'native' implementation, default 100
|
162 | --backoff=<n> Exponential backoff factor for retries (default: 1.2)
|
163 | ```
|
164 |
|
165 | ## mkdirp
|
166 |
|
167 | If you need to _create_ a directory recursively, check out
|
168 | [mkdirp](https://github.com/isaacs/node-mkdirp).
|