- Notifications
You must be signed in to change notification settings - Fork253
isaacs/rimraf
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
TheUNIX commandrm -rf
for nodein a cross-platform implementation.
Install withnpm install rimraf
.
- Require node
20
or>=22
- Add
--version
to CLI
- There is no default export anymore. Import the functions directlyusing, e.g.,
import { rimrafSync } from 'rimraf'
.
- The function returns a
Promise
instead of taking a callback. - Globbing requires the
--glob
CLI option orglob
option propertyto be set. (Removed in 4.0 and 4.1, opt-in support added in 4.2.) - Functions take arrays of paths, as well as a single path.
- Native implementation used by default when available, except onWindows, where this implementation is faster and more reliable.
- New implementation on Windows, falling back to "move thenremove" strategy when exponential backoff for
EBUSY
fails toresolve the situation. - Simplified implementation on POSIX, since the Windowsaffordances are not necessary there.
- As of 4.3, return/resolve value is boolean instead of undefined.
Hybrid module, load either withimport
orrequire()
.
// 'rimraf' export is the one you probably want, but other// strategies exported as well.import{rimraf,rimrafSync,native,nativeSync}from'rimraf'// orconst{ rimraf, rimrafSync, native, nativeSync}=require('rimraf')
All removal functions return a boolean indicating that allentries were successfully removed.
The only case in which this will not returntrue
is ifsomething was omitted from the removal via afilter
option.
This first parameter is a path or array of paths. The secondargument is an options object.
Options:
preserveRoot
: If set to booleanfalse
, then allow therecursive removal of the root directory. Otherwise, this isnot allowed.tmp
: Windows only. Temp folder to place files andfolders for the "move then remove" fallback. Must be on thesame physical device as the path being deleted. Defaults toos.tmpdir()
when that is on the same drive letter as the pathbeing deleted, or${drive}:\temp
if present, or${drive}:\
if not.maxRetries
: Windows and Native only. Maximum number ofretry attempts in case ofEBUSY
,EMFILE
, andENFILE
errors. Default10
for Windows implementation,0
for Nativeimplementation.backoff
: Windows only. Rate of exponential backoff for asyncremoval in case ofEBUSY
,EMFILE
, andENFILE
errors.Should be a number greater than 1. Default1.2
maxBackoff
: Windows only. Maximum total backoff time in ms toattempt asynchronous retries in case ofEBUSY
,EMFILE
, andENFILE
errors. Default200
. With the default1.2
backoffrate, this results in 14 retries, with the final retry beingdelayed 33ms.retryDelay
: Native only. Time to wait between retries, usinglinear backoff. Default100
.signal
Pass in an AbortSignal to cancel the directoryremoval. This is useful when removing large folder structures,if you'd like to limit the time spent.Using a
signal
option prevents the use of Node's built-infs.rm
because that implementation does not support abortsignals.glob
Boolean flag to treat path as glob pattern, or an objectspecifyingglob
options.filter
Method that returns a boolean indicating whether thatpath should be deleted. With asyncrimraf
methods, this mayreturn a Promise that resolves to a boolean. (Since Promisesare truthy, returning a Promise from a sync filter is the sameas just not filtering anything.)The first argument to the filter is the path string. Thesecond argument is either a
Dirent
orStats
object for thatpath. (The first path explored will be aStats
, the restwill beDirent
.)If a filter method is provided, it willonly remove entriesif the filter returns (or resolves to) a truthy value. Omittinga directory will still allow its children to be removed, unlessthey are also filtered out, but any parents of a filtered entrywill not be removed, since the directory will not be empty inthat case.
Using a filter method prevents the use of Node's built-in
fs.rm
because that implementation does not support filtering.
Any other options are provided to the native Node.jsfs.rm
implementationwhen that is used.
This will attempt to choose the best implementation, based on the Node.jsversion andprocess.platform
. To force a specific implementation, useone of the other functions provided.
Synchronous form ofrimraf()
Note that, unlike many file system operations, the synchronous form willtypically be significantlyslower than the async form, because recursivedeletion is extremely parallelizable.
Uses the built-infs.rm
implementation that Node.js provides. This isused by default on Node.js versions greater than or equal to14.14.0
.
Synchronous form ofrimraf.native
Use the JavaScript implementation appropriate for your operating system.
Synchronous form ofrimraf.manual()
JavaScript implementation of file removal appropriate for Windowsplatforms. Works aroundunlink
andrmdir
not being atomicoperations, andEPERM
when deleting files with certainpermission modes.
First deletes all non-directory files within the tree, and thenremoves all directories, which should ideally be empty by thattime. When anENOTEMPTY
is raised in the second pass, fallsback to therimraf.moveRemove
strategy as needed.
Synchronous form ofrimraf.windows()
Moves all files and folders to the parent directory ofpath
with a temporary filename prior to attempting to remove them.
Note that, in cases where the operation fails, thismay leavefiles lying around in the parent directory with names like.file-basename.txt.0.123412341
. Until the Windows kernelprovides a way to perform atomicunlink
andrmdir
operations,this is, unfortunately, unavoidable.
To move files to a different temporary directory other than theparent, provideopts.tmp
. Note that thismust be on the samephysical device as the folder being deleted, or else theoperation will fail.
This is the slowest strategy, but most reliable on Windowsplatforms. Used as a last-ditch fallback byrimraf.windows()
.
Synchronous form ofrimraf.moveRemove()
rimraf version 6.0.1Usage: rimraf <path> [<path> ...]Deletes all files and folders at "path", recursively.Options: -- Treat all subsequent arguments as paths -h --help Display this usage info --version Display version --preserve-root Do not remove '/' recursively (default) --no-preserve-root Do not treat '/' specially -G --no-glob Treat arguments as literal paths, not globs (default) -g --glob Treat arguments as glob patterns -v --verbose Be verbose when deleting files, showing them as they are removed. Not compatible with --impl=native -V --no-verbose Be silent when deleting files, showing nothing as they are removed (default) -i --interactive Ask for confirmation before deleting anything Not compatible with --impl=native -I --no-interactive Do not ask for confirmation before deleting --impl=<type> Specify the implementation to use: rimraf: choose the best option (default) native: the built-in implementation in Node.js manual: the platform-specific JS implementation posix: the Posix JS implementation windows: the Windows JS implementation (falls back to move-remove on ENOTEMPTY) move-remove: a slow reliable Windows fallbackImplementation-specific options: --tmp=<path> Temp file folder for 'move-remove' implementation --max-retries=<n> maxRetries for 'native' and 'windows' implementations --retry-delay=<n> retryDelay for 'native' implementation, default 100 --backoff=<n> Exponential backoff factor for retries (default: 1.2)
If you need tocreate a directory recursively, check outmkdirp.