ItSolutionStuff.com

How to Delete Directory in Node.js?

By Hardik Savani • September 17, 2021
Node JS

Hello,

Today, how to delete directory in node.js is our main topic. you'll learn node js delete folder if exists. In this article, we will implement a node js delete folder with files. it's simple example of node js delete directory.

We will use fs npm package for delete folder using node.js. fs package provide rmdirSync() for remove folder. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure, you have created "images" folder with some images.

server.js

const fs = require('fs');

var folder = './images';

if (fs.existsSync(folder)){

fs.rmdirSync(folder, { recursive: true });

console.log('Folder Deleted Successfully.');

}

now you can simply run by following command:

node server.js

Output:

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check File is Exist or Not in Node JS?

Read Now →

How to Create Directory if does not Exists in Node JS?

Read Now →

How to Delete File If Exists in Node JS?

Read Now →

How to Get All Files from Folder in Node JS?

Read Now →

How to Download Image from URL in Node JS?

Read Now →

How to Generate QR Code in Node JS?

Read Now →

How to Get Data from Json File in Node JS?

Read Now →

How to Remove Element from Array in Node JS?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

Node JS Foreach Loop Array Example

Read Now →

How to Create and Use .env File in Node JS?

Read Now →

Node JS Make HTTP Put Request Example

Read Now →