Answers for "nodejs delete files in directory"

2

node js delete folder with files

const fs = require('fs');

// directory path
const dir = 'temp';

// delete directory recursively
try {
    fs.rmdirSync(dir, { recursive: true });

    console.log(`${dir} is deleted!`);
} catch (err) {
    console.error(`Error while deleting ${dir}.`);
}
Posted by: Guest on July-23-2021
1

delete all files in a folder with nodejs

rmDir = function(dirPath, removeSelf) {
  try { var files = fs.readdirSync(dirPath); }
  catch(e) { return; }
  if (files.length > 0)
    for (var i = 0; i < files.length; i++) {
      var filePath = dirPath + '/' + files[i];
      if (fs.statSync(filePath).isFile())
        fs.unlinkSync(filePath);
      else
        rmDir(filePath);
    }
  if (removeSelf)
    fs.rmdirSync(dirPath);
};
Posted by: Guest on June-23-2021
0

how to delete a folder using node js

fs.unlink('./node/myText.txt',function(){
    fs.rmdir('node');
});
Posted by: Guest on February-12-2021
0

how to delete everything from a folder in js

const fsPromises = require('fs').promises 
// For ES syntax: import { promises as fsPromises } from 'fs'

const directory = 'your/directory/path/here'

await fsPromises.rmdir(directory, {
  recursive: true
})
Posted by: Guest on August-11-2020

Code answers related to "nodejs delete files in directory"

Code answers related to "Javascript"

Browse Popular Code Answers by Language