How to Delete Directory in Node.js?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

Hello Brother, 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 lesson 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 lesson

Lesson 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Lesson 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

Result:-

thank you very much for following up with me.

Tags

Recent Posts

Related Posts