How to delete folder in Node.js? fs.rmdir() – Node.js

Introduction

In this blog post, we will learn how to delete a folder programmatically in Node.js. Although there are a lot of other ways to delete folders in Node.js one common way is to use a system command. Node.js provides child_process module to execute shell commands. It is not advisable to use child_process module which does a system call as system call is always CPU expensive instead of this one should use core APIs for such operations. Node.js core fs module provides methods to delete the directory from the file system. fs.rmdir()  removes a folder or directory asynchronously. fs.rmdirSync() remove a folder or directory synchronously. Both of the APIs can be used to delete nested directories as well.

fs.rmdir()

fs.rmdir() is an asynchronous method to delete a directory at the given path.

Syntax of fs.rmdir()

fs.rmdir(path[, options], callback)

This method takes three parameters as described below:

  • path: Name of the path of the directory that has to be removed. It can be a String, Buffer or URL.
  • options: An object that can be used to specify optional parameters.
    • maxRetries: It is an integer value that specifies the number of times Node.js will try to perform the operation when it fails due to an error.
    • recursive: If true, perform a recursive directory removal. In recursive mode, operations are retried on failure. Default: false
    • retryDelay: The amount of time in milliseconds to wait between retries. This option is ignored if the recursive option is not trueDefault: 100.
  • callback: It is a function that would be called when the method is executed.
    • err: error object. (If there is no error, error object holds null value)

Following code snippet illustrates the fs.rmdir() in Node.js:

// import node fs module
const fs = require('fs');
 
// function to delete directory asynchronously
fs.rmdir('/tmp/foo', function (err) {
    if (err) throw err;
    console.log('directory deleted successfully.');
});

fs.rmdirSync()

fs.rmdirSync() is synchronous method to delete a directory at the given path.

Syntax of fs.rmdirSync()

fs.rmdirSync(path[, options])

This method takes two parameters as described below:

  • path: Name of the path of the directory that has to be removed. It can be a String, Buffer or URL.
  • options: An object that can be used to specify optional parameters.
    • maxRetries: It is an integer value that specifies the number of times Node.js will try to perform the operation when it fails due to an error.
    • recursive: If true, perform a recursive directory removal. In recursive mode, operations are retried on failure. Default: false
    • retryDelay: The amount of time in milliseconds to wait between retries. This option is ignored if the recursive option is not trueDefault: 100.

Following code snippet illustrates the fs.rmdirSync() in Node.js:

// import node fs module
const fs = require('fs');
 
// function to delete directory synchronously
fs.rmdirSync('/tmp/foo');
console.log('Directory deleted successfully !');

Conclusion

It is recommended to use an asynchronous function instead of synchronous. As synchronous function blocks the event loop and should not be used in the production environment. If you want to delete only files then you can use fs.unlink() or fs.unlinkSync() methods.

If you want to use some library rather than core feature of Node.js, There is a module for this named as rimraf (https://npmjs.org/package/rimraf). It provides the same functionality as rm -rf.

Async Usage:

let rimraf = require("rimraf");
rimraf("/some/folde/", function () { console.log("done"); });

Sync Usage:

rimraf.sync("/some/folder");

Thanks for reading.

#Happy Coding #TopCode

#delete folder in node.js #delete directory in Node.js #rmdir nodejs #unlink node js #delete directory programatically in node.js #delete a folder with node.js #fs module delete folder #nodejs delete directory #nodejs delete folder #nodejs delete folder using fs module #fs module api #fs module node.js explained #fs module nodejs #delete folder with fs module #fs module delete directory #fs.unlinksync nodejs #fs.rmdir node.js #fs.rmdirsync node.js #remove directory nodejs fs modue #nodejs remove directory fs module #fs module nodejs remove folder #fs module nodejs remove directory #fs module nodejs delete folder #fs module nodejs delete directory #How to delete a non-empty directory in Node.js #delete a non-empty directory in Node.js #Remove Both Empty & Non-Empty Directories Using Node.js #Node.js | fs.rmdir() Method #How to delete folders and files in JavaScript/ NodeJS #how to delete folder in node js #delete directory javascript #delete folder program node.js

, , , , ,

Related posts

Latest posts

1 comment

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site