Are you working with system files in Node.js and need a way to remove a directory? And even one that contains files and additional sub-directories?

In this article, we'll cover how to remove both an empty directory and a directory that has multiple files and sub-directories.

For the code in this article to work, make sure you have Node.js installed and a directory you wish to remove.

Let's get into it!

Table of Contents

Remove an Empty Directory

First, let's cover how to remove an empty directory.

Luckily, Node.js has a built-in way to do this with the File System (Fs) core module, which has a fs.rmdir(path, callback) method that will remove an empty directory.

Here is what the code looks like:

      const fs = require("fs")
      const path = require("path")

      const pathToDir = path.join(__dirname, "your-directory")

      fs.rmdir(pathToDir, function(err) {
        if (err) {
          throw err
        } else {
          console.log("Successfully removed the empty directory!")

Let's break down each part of the code:

  1. First, we import the Fs module and Path core modules.
  2. Next, we create the pathToDir variable. We use the Path module to get the path of the directory we wish to remove.
  3. Then, we use the fs.rmdir() function. We pass the directory path to the function and it returns a callback.
  4. Inside the callback function, we do some error handling and then, if successful, we console.log() a success message.

When you run the code in your terminal, you should see this output:

      Successfully removed the empty directory!

And the directory will be removed.

Remove a Directory With Files & Sub-Directories

What if we need to remove a directory that has files and sub-directories inside it? And how do we recursively go through each sub-directory and remove it's children files and directories.

Below we have written a function that recursively moves through a directory and all its sub-directories, deletes every file, and then deletes the parent directory when complete.

We'll give you the full code and explain everything afterward.

Here's the code:

      const fs = require("fs")
      const path = require("path")

      const removeDir = function(path) {
        if (fs.existsSync(path)) {
          const files = fs.readdirSync(path)

          if (files.length > 0) {
            files.forEach(function(filename) {
              if (fs.statSync(path + "/" + filename).isDirectory()) {
                removeDir(path + "/" + filename)
              } else {
                fs.unlinkSync(path + "/" + filename)
          } else {
        } else {
          console.log("Directory path not found.")

      const pathToDir = path.join(__dirname, "directory-name")


A lot is going on here, so let's break down each section.

The removeDir function is what we use to recursively remove a parent directory and it's files and sub-directories. The only parameter is the path to the parent directory.

Inside the removeDir function, the first thing we do is use the fs.existsSync() to verify that the directory we passed to our function exists. If it doesn't exist, we end the function and log an error message.

Then, we pass our directory path to the fs.readdirSync() function to get an array of all the files and sub-directories inside the parent directory. And we store that array in a variable named files.

If the array is empty, that means the directory is empty and we can use the fs.rmdirSync() function to delete it.

If the array.length > 0, we loop through the files array with the forEach() function. For each item in the files array, we check whether or not the item is a file or a sub-directory.

If the item is a sub-directory, we have the function recursively call itself with removeDir(), with the path of the sub-directory as a parameter. That function will loop through the sub-directory and remove it's children files and directories.

And if the item is a file, we simply use the fs.unlinkSync() to remove the file, with the path to the file given as a parameter.

When the .forEach() loop has finished and all the files have been removed, you can delete the now empty directory with the fs.rmdirSync() function.

Then we call our removeDir(pathToDir) function with the path to the parent directory as a parameter.

When you run the code, you should see that the directory in question is removed.

If you don't want to deal with writing your own function for this, there are is the Fs-Extra NPM package that makes this much easier to do.

That package provides functionality built specifically for this problem.


Hopefully, this article helped you in your coding endeavors.

Thanks for reading and happy coding!