22-Sep-2022
.
Admin
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!
I hope it can help you...
#Node JS