delete dir nodejs
fs.rmdirSync(dir, { recursive: true });
delete dir nodejs
fs.rmdirSync(dir, { recursive: true });
how to delete all json files in a directory nodejs
// delete all json files in same directory as nodejs script, or subdirectory.
const directoryPath = path.join(__dirname, "sub_directory_if_needed");
function DeleteFiles() {
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
if (file !== "package.json") { // Making sure not to delete package.json, as it should not be deleted
if (file.endsWith(".json")) {
fs.unlink(`${directoryPath}/${file}`, function(err) {
if (err) throw err
console.log(file, "deleted")
})
}
}
});
});
}
DeleteFiles()
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us