UNPKG

6.26 kBMarkdownView Raw
1The [UNIX command](<http://en.wikipedia.org/wiki/Rm_(Unix)>) `rm -rf` for node.
2
3Install 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
20Hybrid module, load either with `import` or `require()`.
21
22```js
23// default export is the main rimraf function
24import rimraf from 'rimraf'
25// or
26const rimraf = require('rimraf').default
27
28// other strategies exported as well
29import { rimraf, rimrafSync, native, nativeSync } from 'rimraf'
30// or
31const { rimraf, rimrafSync, native, nativeSync } = require('rimraf')
32```
33
34### `rimraf(f, [opts]) -> Promise`
35
36This first parameter is a path or array of paths. The second
37argument is an options object.
38
39Options:
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
65Any other options are provided to the native Node.js `fs.rm` implementation
66when that is used.
67
68This will attempt to choose the best implementation, based on Node.js
69version and `process.platform`. To force a specific implementation, use
70one of the other functions provided.
71
72### `rimraf.sync(f, [opts])` `rimraf.rimrafSync(f, [opts])`
73
74Synchronous form of `rimraf()`
75
76Note that, unlike many file system operations, the synchronous form will
77typically be significantly _slower_ than the async form, because recursive
78deletion is extremely parallelizable.
79
80### `rimraf.native(f, [opts])`
81
82Uses the built-in `fs.rm` implementation that Node.js provides. This is
83used 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
87Synchronous form of `rimraf.native`
88
89### `rimraf.manual(f, [opts])`
90
91Use the JavaScript implementation appropriate for your operating system.
92
93### `rimraf.manualSync(f, [opts])` `rimraf.manualSync(f, opts)`
94
95Synchronous form of `rimraf.manual()`
96
97### `rimraf.windows(f, [opts])`
98
99JavaScript implementation of file removal appropriate for Windows
100platforms. Works around `unlink` and `rmdir` not being atomic
101operations, and `EPERM` when deleting files with certain
102permission modes.
103
104First deletes all non-directory files within the tree, and then
105removes all directories, which should ideally be empty by that
106time. When an `ENOTEMPTY` is raised in the second pass, falls
107back to the `rimraf.moveRemove` strategy as needed.
108
109### `rimraf.windows.sync(path, [opts])` `rimraf.windowsSync(path, [opts])`
110
111Synchronous form of `rimraf.windows()`
112
113### `rimraf.moveRemove(path, [opts])`
114
115Moves all files and folders to the parent directory of `path`
116with a temporary filename prior to attempting to remove them.
117
118Note that, in cases where the operation fails, this _may_ leave
119files lying around in the parent directory with names like
120`.file-basename.txt.0.123412341`. Until the Windows kernel
121provides a way to perform atomic `unlink` and `rmdir` operations,
122this is unfortunately unavoidable.
123
124To move files to a different temporary directory other than the
125parent, provide `opts.tmp`. Note that this _must_ be on the same
126physical device as the folder being deleted, or else the
127operation will fail.
128
129This is the slowest strategy, but most reliable on Windows
130platforms. Used as a last-ditch fallback by `rimraf.windows()`.
131
132### `rimraf.moveRemove.sync(path, [opts])` `rimraf.moveRemoveSync(path, [opts])`
133
134Synchronous form of `rimraf.moveRemove()`
135
136### Command Line Interface
137
138```
139rimraf version 4.0.4
140
141Usage: rimraf <path> [<path> ...]
142Deletes all files and folders at "path", recursively.
143
144Options:
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
158Implementation-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
167If you need to _create_ a directory recursively, check out
168[mkdirp](https://github.com/isaacs/node-mkdirp).