Node Js Delete Directory or Folder Tutorial

Removing files and folders is not a challenging task, however sometimes we have to programmatically remove a folder and directory from Node application.

Today, In this article we would like to show you the real and simple approach to remove empty or non empty directory in Node js application.

To delete the directory or folder in node application, we will need file system and file path modules.

Before you start going through this tutorial, make sure to install latest version of Node and Npm in your system. We are building this tutorial on Node version 17.

How to Remove Empty or Non Empty Directory in Node Js

  • Step 1: Create Project Folder
  • Step 2: Build Node App
  • Step 3: Create Server File
  • Step 4: Remove Node Directory

Create Project Folder

First, make the empty folder. In this folder, we will keep all the project related files.

Make sure to open the terminal and execute the command as given below.

You may create it manually if you want.

mkdir node-man

Lets, get inside the project folder.

cd node-man

Build Node App

In the next step, we will run the npm initializer command. This command set up the ground for node js app.

npm init

After you execute the command, some questions will appear on your terminal screen make sure to provide information.

It will generate a new package.json file inside the project directory.

In the next, step, you have to create the server.js file in node app folder.

Also, ensure that you create a new assets folder in node app, this is the node app folder we will teach you to remove from node project.

Create Server File

We are now going to write the function which will remove the empty or non empty directory in node js application.

We need to import the fs (file system) and path modules into the server.js file.

Define the variable, it must hold the name of the folder that has to be removed from the node app.

With the help of the fs module access the rmdir method, this method allows you to remove the folder just pass the directory name as a first parameter.

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

const directoryPath = path.join(__dirname, 'assets')

fs.rmdir(directoryPath, function (error) {
  if (error) {
    throw error
  } else {
    console.log('Folder deleted successfully!')
  }
})

Remove Node Directory

In the previous step, we created a server file, added some code that will delete the directory.

Now, in this segment we will register the server file name under the main property inside the package.json file.

{
  "main": "server.js",
}

We are now ready to execute the command from the terminal.

Head over to terminal, type the given command and hit enter.

node server.js

When the folder is deleted, following message will appear on your terminal.

Folder deleted successfully!

Node Js Delete Directory or Folder Tutorial

Summary

In app development, you may not know what sort of feature you need to build for your clients.

This article covered the topic of how to delete a directory in Node.js.

We showed you step by step how to use the required modules to delete folders (non empty or empty) in node js.

We truly believe this simple guide will help in your programming journey and has helped you learn build a simple feature in node js.