How to Delete Folder with Files in Node.js?
Sep 22, 2022
Hello Friends,
In this example, you will learn how to delete a folder with files in node.js. We will look at an example of node.js delete folder with files. This article will give you a simple example of nodejs delete folder with files. this example will help you delete the node js folder with files.
I will explain this example delete folder with files. In this example delete folder with files using fs(File System) package and rmdirSync() in nodejs.
So, let's start following example with output:
Step 1: Install Node JS
This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:
mkdir my-app
cd my-app
npm init
Step 2: Update server.js file
server.js
const fs = require('fs');
// directory path
const dir = 'destination';
// delete directory recursively
try {
fs.rmdirSync(dir, { recursive: true });
console.log(`${dir} is deleted!`);
} catch (err) {
console.error(`Error while deleting ${dir}.`);
}
Output:
destination is deleted!
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?