Answers for "node js how to delete file in folder"

7

nodejs fs delete file

const fs = require('fs')

const path = './file.txt'

try {
  fs.unlinkSync(path)
  //file removed
} catch(err) {
  console.error(err)
}
Posted by: Guest on February-10-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

Code answers related to "node js how to delete file in folder"

Code answers related to "Javascript"

Browse Popular Code Answers by Language