Node Remove Files with Directory / Sub Directory Tutorial

In this step by step tutorial, we will reveal how to dynamically delete folder, files and sub folders in Node js application using the fs and path modules.

Building essential functionalities in Node js environment is not difficult, thanks to its community.

Node provides plethora of packages that make your life easy. This very support makes node js quintessentials for web development. Working with node is easy, in this guide we will show you how to:

Create and setup a brand new node js project.

How to remove folders and sub folders programmatically from scratch.

How to Delete Folder with Files and Sub Folders in Node Js

Here are the steps define that will help you recursively check every folder, sub folder and delete all the child files located inside of it.

  • Step 1: Create Folder
  • Step 2: Set Up Node Project
  • Step 3: Configure App File
  • Step 4: Remove Files Folder and Sub Folders

Create Folder

In the first step, we will dynamically create a blank folder. Why?

This folder will be the main directory of your node project. All the project related files will be formed here.

Without further ado, execute the given command and manifest the app folder on your system.

You may create it manually if you want.

mkdir node-pro

As soon as the directory is created, make sure to go inside of it.

cd node-pro

Set Up Node Project

Setting up a new node project is easy, it requires a package.json file.

A package file contains the significant information about the scripts, packages and project version.

You just have to invoke the given command from the terminal:

npm init

Now, you need to make the server.js file in your node project directory.

Its just a basic project, to show you the demo of removing files and folder we need a folder.

Therefore, make a new asset-folder folder, create a sub-folder and also keep some file inside this folder.

Next, you have to add the server file name inside the main property within the package.json file.

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

Configure App File

Make sure to import the fs and path modules at the top of the file.

To remove the files and folder, the deleteFolder function is recursively checking the files, sub-directory and main directory.

Hence, you have to open the server.js and add the given code into the file.

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

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

    if (files.length > 0) {
      files.forEach(function (fileSrc) {
        if (fs.statSync(path + '/' + fileSrc).isDirectory()) {
          deleteFolder(path + '/' + fileSrc)
        } else {
          fs.unlinkSync(path + '/' + fileSrc)
        }
      })
      fs.rmdirSync(path)
    } else {
      fs.rmdirSync(path)
    }
  } else {
    console.log('Folder does not exist.')
  }
}

const pathToDir = path.join(__dirname, 'asset-folder')

deleteFolder(pathToDir)

Remove Files Folder and Sub Folders

Open the terminal and type the command on the command prompt.

node server.js

After you press enter, the files, folder and sub-folder will be deleted.

Folder deleted successfully!

Node Remove Files with Directory / Sub Directory Tutorial

Summary

In this short guide we have tried to answer how to delete files, folders and sub folders using the command line tool.

To build this feature we used the default node modules file system and path.

We literally believe this guide will help in your development journey.

Have a nice day keep coding.