del vs fs-extra vs remove vs rimraf
File and Directory Deletion Strategies in Node.js
delfs-extraremoverimrafSimilar Packages:

File and Directory Deletion Strategies in Node.js

del, fs-extra, remove, and rimraf are Node.js utilities designed to handle file and directory deletion, a task that the native fs module makes cumbersome due to its lack of recursive removal support in older versions and asynchronous complexity. fs-extra is a comprehensive drop-in replacement for the native fs module that adds recursive delete capabilities alongside many other file operations. rimraf is a specialized, high-performance utility focused deeply on removing files and directories recursively, often used internally by other tools. del is a higher-level wrapper around rimraf that supports glob patterns for matching multiple files. remove is a simpler utility that has seen less maintenance activity compared to the others. Choosing the right tool depends on whether you need glob matching, a full file system utility suite, or a lightweight, dedicated deletion tool.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
del01,34412.7 kB176 months agoMIT
fs-extra09,62257.7 kB1223 days agoMIT
remove011-314 years agoMIT
rimraf05,842262 kB8a month agoBlueOak-1.0.0

File and Directory Deletion Strategies in Node.js: del vs fs-extra vs remove vs rimraf

Deleting files and directories in Node.js used to be a pain point. The native fs module required you to write recursive logic manually to remove a folder with contents. Over time, several packages emerged to solve this. del, fs-extra, remove, and rimraf are the most common solutions. They all aim to make deletion easy, but they serve different architectural needs. Let's compare how they handle real-world deletion tasks.

🗑️ Basic Deletion: Removing a Single Directory

All four packages allow you to remove a directory recursively. However, the API style and promise handling differ.

del uses a Promise-based API and accepts paths or patterns.

import del from 'del';

// Deletes the 'dist' directory
await del('dist');

fs-extra mirrors the native fs API but adds recursive support to remove.

import fs from 'fs-extra';

// Deletes the 'dist' directory recursively
await fs.remove('dist');

remove provides a simple function call for deletion.

import remove from 'remove';

// Deletes the 'dist' directory
remove('dist', function (err) {
  if (err) throw err;
  console.log('deleted');
});

rimraf is often used via its Promise wrapper or CLI, but the core is callback-based (though rimraf@5 supports promises natively).

import rimraf from 'rimraf';

// Deletes the 'dist' directory
await rimraf('dist');

🎯 Pattern Matching: Glob Support

One of the biggest differentiators is whether you can delete multiple files using patterns (globs) like *.log or **/tmp.

del has glob support built-in. This is its main selling point.

import del from 'del';

// Deletes all JS files in the dist folder
await del('dist/**/*.js');

fs-extra does not support globs natively. You must combine it with a library like globby.

import fs from 'fs-extra';
import { globby } from 'globby';

const files = await globby('dist/**/*.js');
await Promise.all(files.map(file => fs.remove(file)));

remove does not support globs. It handles single paths only.

import remove from 'remove';

// No glob support, must pass exact path
remove('dist/file.js', callback);

rimraf does not support globs natively in its core API. It expects a specific path.

import rimraf from 'rimraf';

// No glob support, must pass exact path
await rimraf('dist/file.js');

⚙️ API Style: Promises vs Callbacks

Modern Node.js development favors Promises and async/await. Legacy callback patterns can lead to "callback hell".

del is Promise-first. It returns a Promise by default.

import del from 'del';

// Returns a Promise
const deletedPaths = await del(['tmp', 'log']);

fs-extra supports both Promises and Callbacks. It feels like native fs.

import fs from 'fs-extra';

// Promise style
await fs.remove('tmp');

// Callback style
fs.remove('tmp', err => { /*...*/ });

remove is primarily Callback-based. This makes it feel older compared to modern alternatives.

import remove from 'remove';

// Callback style required
remove('tmp', (err) => {
  if (err) console.error(err);
});

rimraf has evolved. Version 5+ is Promise-first, but older versions were callback-heavy.

import rimraf from 'rimraf';

// Modern Promise style (v5+)
await rimraf('tmp');

🛡️ Safety and Options: Dry Runs and Protection

Accidental deletion is a major risk in build scripts. Some packages offer options to prevent data loss.

del supports a dryRun option to see what would be deleted without actually deleting.

import del from 'del';

// Logs what would be deleted without removing
await del('dist', { dryRun: true });

fs-extra does not have a built-in dryRun option for remove. You must implement logic yourself.

import fs from 'fs-extra';

// No built-in dryRun, must check existence manually
if (await fs.pathExists('dist')) {
  // Manually implement dry run logic
}

remove lacks advanced safety options like dryRun.

import remove from 'remove';

// No options for dry run
remove('dist', callback);

rimraf focuses on performance and reliability, not safety features like dryRun.

import rimraf from 'rimraf';

// No built-in dryRun option
await rimraf('dist');

🌐 Real-World Scenarios

Scenario 1: Cleaning Build Artifacts

You need to clear the dist folder before every build, including nested temp files.

  • Best choice: del
  • Why? You can use del('dist') or del(['dist/**/*']) easily. The dryRun option helps debug CI scripts.
import del from 'del';
await del('dist');

Scenario 2: General File System Utility

Your app needs to copy, move, and delete files regularly.

  • Best choice: fs-extra
  • Why? You get remove, copy, move, and ensureDir in one package. No need to mix libraries.
import fs from 'fs-extra';
await fs.remove('old-data');
await fs.copy('new-data', 'current-data');

Scenario 3: Internal CLI Tool

You are building a CLI that needs to wipe a cache directory reliably.

  • Best choice: rimraf
  • Why? It is battle-tested, lightweight, and specifically optimized for removal. Many other tools use it under the hood.
import rimraf from 'rimraf';
await rimraf('.cache');

Scenario 4: Legacy Codebase

You are maintaining an older Node.js app that relies on callbacks.

  • ⚠️ Consider: remove or rimraf (older versions)
  • Why? If you cannot refactor to Promises, these fit the callback pattern. However, upgrading to fs-extra is recommended for long-term health.
import remove from 'remove';
remove('temp', (err) => { /* handle */ });

📌 Summary Table

Featuredelfs-extraremoverimraf
Primary UseGlob-based deletionFull fs replacementBasic deletionRecursive deletion
Glob Support✅ Yes❌ No (use globby)❌ No❌ No
API StylePromisePromise + CallbackCallbackPromise (v5+)
Dry Run✅ Yes❌ No❌ No❌ No
Maintenance✅ Active✅ Very Active⚠️ Low Activity✅ Very Active
DependenciesWraps rimrafNone (native wrapper)MinimalMinimal

💡 Final Recommendation

Think about the scope of your file operations:

  • Need to delete by pattern? → Use del. It saves you from writing glob logic manually.
  • Need a full file system toolkit? → Use fs-extra. It standardizes all your file I/O.
  • Need a dedicated deletion tool? → Use rimraf. It is the industry standard for recursive removal.
  • Avoid remove for new projects. It lacks the active maintenance and feature set of the others.

Final Thought: For most modern frontend build pipelines, del is the most convenient for cleaning tasks. For application logic involving file manipulation, fs-extra provides the best consistency. rimraf remains the engine under the hood for many of these tools, making it a solid dependency if you want to minimize abstraction layers.

How to Choose: del vs fs-extra vs remove vs rimraf

  • del:

    Choose del if you need to delete multiple files or directories using glob patterns (e.g., dist/**/*.js). It is ideal for build scripts where you need to clean up artifacts matching specific patterns without writing complex traversal logic. It wraps rimraf internally, so you get its reliability with added convenience for matching.

  • fs-extra:

    Choose fs-extra if you want a complete replacement for the native fs module that includes remove alongside other utilities like copy, move, and ensureDir. It is best for applications that need a broad set of file system operations without mixing native fs calls with third-party helpers.

  • remove:

    Avoid remove for new projects as it has significantly less maintenance activity and community adoption compared to rimraf or fs-extra. It offers basic recursive deletion but lacks the robust ecosystem and long-term support guarantees of the other options. Stick to more actively maintained alternatives for production systems.

  • rimraf:

    Choose rimraf if you need a dedicated, high-performance tool specifically for recursive deletion without the overhead of a larger library. It is the standard choice for internal tooling, CLI packages, and scenarios where you need to ensure a directory is wiped clean reliably across different operating systems.

README for del

del

Delete files and directories using globs

Similar to 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.

Install

npm install del

Usage

import {deleteAsync} from 'del';

const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);

console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));

Beware

The glob pattern ** matches all children and the parent.

So this won't work:

deleteSync(['public/assets/**', '!public/assets/goat.png']);

You have to explicitly ignore the parent directories too:

deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);

To delete all subdirectories inside public/, you can do:

deleteSync(['public/*/']);

Suggestions on how to improve this welcome!

API

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().

deleteAsync(patterns, options?)

Returns Promise<string[]> with the deleted paths.

deleteSync(patterns, options?)

Returns string[] with the deleted paths.

patterns

Type: string | string[]

See the supported glob patterns.

options

Type: object

You can specify any of the globby options in addition to the below options. In contrast to the globby defaults, expandDirectories, onlyFiles, and followSymbolicLinks are false by default.

force

Type: boolean
Default: false

Allow deleting the current working directory and outside.

dryRun

Type: boolean
Default: false

See what would be deleted.

import {deleteAsync} from 'del';

const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});

console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
dot

Type: boolean
Default: false

Allow patterns to match files/folders that start with a period (.).

This option is passed through to fast-glob.

Note that an explicit dot in a portion of the pattern will always match dot files.

Example

directory/
├── .editorconfig
└── package.json
import {deleteSync} from 'del';

deleteSync('*', {dot: false});
//=> ['package.json']
deleteSync('*', {dot: true});
//=> ['.editorconfig', 'package.json']
concurrency

Type: number
Default: Infinity
Minimum: 1

Concurrency limit.

onProgress

Type: (progress: ProgressData) => void

Called after each file or directory is deleted.

import {deleteAsync} from 'del';

await deleteAsync(patterns, {
	onProgress: progress => {
	// …
}});
ProgressData
{
	totalCount: number,
	deletedCount: number,
	percent: number,
	path?: string
}
  • percent is a value between 0 and 1
  • path is the absolute path of the deleted file or directory. It will not be present if nothing was deleted.

CLI

See del-cli for a CLI for this module and trash-cli for a safe version that is suitable for running by hand.

Related

  • make-dir - Make a directory and its parents if needed
  • globby - User-friendly glob matching