Node.js – How to Remove or Delete Directory | fs.rmdir()

  • by
node-js-fs-module-delete-remove-directory

The fs module function fs.rmdir() removes a specified directory, it can be used recursively to remove multiple nested directories. In the previous tutorial we saw how to create directories and parent directories using fs module in node.js. In this post we will learn how to delete or remove a directory and nested directories using fs.

Removing an empty directory using fs.rmdir():

index.js:

const fs = require('fs');
const path = require('path');

fs.rmdir("./newFolder", (err) => {

if (err) {
return console.log("error occurred in deleting directory", err);
}

console.log("Directory deleted successfully");
});

Above code will remove folder with name newFolder from my project’s root directory:

node-js-fs-module-delete-remove-directory

fs.rmdir() is an asynchronous function. This function accepts three params:

  1. path (<string>|<Buffer>|<URL>): The folder name and its path which is to be deleted.
  2. options (<Object>)
    1. maxRetries<integer>: It is an integer value, used to specified the number of times the program will try to delete the directory if the operation fails with a specified retryDelay milliseconds interval. This option is ignored if the recursive option is false. Default value is 0.
    2. recursive<boolean>: If recursive is defined as true, the program will perform a recursive directory removal. In specified true, errors will not be reported if the directory path does not exist. Default is false.
    3. retryDelay <integer>: This declares the time in milliseconds to wait between retries. This option is ignored when recursive is false. Default is 100 milliseconds.
  3. callback <function>: callback function declaration, can be used to catch any possible errors or exceptions.

If we use fs.rmdir() on a file instead of a directory, it will return an ENOENT error on windows and an ENOTDIR error on POSIX

When we run the above code again, it will return an error because the directory is already being deleted and it does not exists now in the parent directory:

starting `node ./index.js`
error occurred in deleting directory [Error: ENOENT: no such file or directory, rmdir ‘D:\nodejs\FileSystem\newFolder’] {
errno: -4058,
code: ‘ENOENT’,
syscall: ‘rmdir’,
path: ‘D:\\nodejs\\FileSystem\\newFolder’
}

To solve this issue, we can specify { recursive: true, force: true } in the options param:

const fs = require('fs');
const path = require('path');

fs.rmdir("./newFolder",
{ recursive: true, force: true }, (err) => {

if (err) {
return console.log("error occurred in deleting directory", err);
}

console.log("Directory deleted successfully");
});

This will not return error if the specified directory is not present. Be Cautious: The above code will delete all the directory and files present inside the specified directory recursively without any warnings or errors.

Removing multiple | nested directories at once using fs.rmdir():

const fs = require('fs');
const path = require('path');

fs.rmdir("./folder1",
{ recursive: true, force: true }, (err) => {

if (err) {
return console.log("error occurred in deleting directory", err);
}

console.log("Directory deleted successfully");
});

Output:

node-js-fs-module-rmdir-remove-delete-nested-directories-directory

To avoid this we can first check if there is any directories or files present inside of the folder which we are trying to delete and stop the execution if there is anything inside of it:

const fs = require('fs');
const path = require('path');

function deleteDirectory(directoryPath) {
let files = fs.readdirSync(directoryPath);

console.log("files", files);

if (files && files.length)
return console.log(directoryPath, "contains", files.length, "folders or files. So it cannot be deleted");

fs.rmdir("./folder1",
{ recursive: true, force: true }, (err) => {

if (err) {
return console.log("error occurred in deleting directory", err);
}

console.log("Directory deleted successfully");
});
}

deleteDirectory('./folder1');

This will return message like “directory folder” contains files or folders. if the directory contains any files or folders and if not it will delete the directory.