nodejs delete folder recursively
To delete a folder recursively in Node.js, you can use the fs-extra
module. Follow these steps:
- Install the
fs-extra
module by running the commandnpm install fs-extra
in your project directory. - Import the module in your code using
const fs = require('fs-extra')
. - Use the
fs.remove()
method to delete the folder recursively. Pass the path of the folder you want to delete as the first argument, and a callback function as the second argument. The callback function will be called once the folder is deleted or if there is an error. Here's an example:
const fs = require('fs-extra');
const folderPath = '/path/to/folder';
fs.remove(folderPath, (err) => {
if (err) {
console.error(err);
} else {
console.log('Folder deleted recursively!');
}
});
In this example, replace /path/to/folder
with the actual path to the folder you want to delete. If the deletion is successful, the message "Folder deleted recursively!" will be logged to the console. If there is an error, it will be logged to the console using console.error()
.
That's it! Following these steps will allow you to delete a folder recursively in Node.js using the fs-extra
module.