How to Remove or Delete All Files in Node Js App Folder

Node offers flexibility in application development; you can create many features using node.

To amplify your development experience, we are going to show you how to remove all files, images from the node application folder.

To make the web application user and developer-friendly, we often create various features.

Seldom, you might get a feature to build where you need to delete all the files located in the node js app directory.

In this quick tutorial, we will teach you how to create a script that removes all files in a project directory recursively.

Let us find out how.

Node Js Delete or Remove All Files in Folder Example

  • Step 1: Create Node Project
  • Step 2: Create Package.json File
  • Step 3: Create App Js File
  • Step 4: Delete All Files from Folder
  • Step 6: Test Node Script

Create Node Project

In the first step we will create a project folder. After typing the command press enter, name it whatever you want.

mkdir node-demo

After the directory generated on your system, next step into the project folder.

cd node-demo

Create Package.json File

In this step we will use the npm command to initiate the process of generating package.json file.

npm init

After executing the above command, a new package.json file generated.

The package json file is the soul of any Node project. It registers the necessary metadata concerning a project which is needed before publishing to NPM.

Create App Js File

Furthermore, you have to create app.js file inside your node project directory. In this file, we will write the code to delete all files from folder.

We have to execute the script through command line, consequently we have to create the node script.

Open the package.json file and update the script section as given:

  "scripts": {
    "start": "node app.js"
  },

Delete All Files from Folder

Before creating the file, remove script, make sure to create /uploads folder and add some images into the folder.

To remove all the files from the node js folder, we will use a simple approach; at the begging, we define a folder path, import the fs module.

The fs the module offers .readdir() method that allows you to read the dir and the content in the directory or folder.

We are using the for loop and iterate over every file or image that exists in the folder and use the unlinkSync method to remove the file.

Update code in app.js file.


const fs = require('fs')

let DIR = './uploads/'

fs.readdir(DIR, (error, filesInDirectory) => {
  if (error) throw error

  for (let file of filesInDirectory) {
    console.log('File removed' + ' : ' + file)
    fs.unlinkSync(DIR + file)
  }
})

Test Node Script

In this step we will test the script, for removing the files you have to type and execute the following command from the terminal.

node app.js

If everything is implemented appropriately, you would see something similar output on the console screen:

# File removed : john-doe-01.jpeg

# File removed : john-doe-02.jpeg

Summary

How to Remove or Delete All Files in Node Js App Folder

We always try to bring something new regarding node js development today. In this tutorial, we covered two essential methods readdir() and unlinkSync().

We used these node methods to delete all files from the folder in the node javascript project.

We believe you will indeed like this quick guide and share your feedback with us.