0

How do I delete a file with node.js?
folder structure

 -> .git
    MachineFolder(non emtey)
    READEME.md

I try to delete this code: -

 fs.readdir(dir, (err, files) => {
  if (err) throw err;
  Logger.log(files)
  try {
    for (const file of files) {
      console.log('file',file)
      fs.unlink(path.join(dir, file), (err) => {
        return `${err}`
      });
    }
    return 'done'
    // success code here
  } catch (err) {
    // error handling here
    console.error(err)
  }
  
})

only the README.md file can be deleted. Anyone can help solve this problem.

JaNith RathNayaka
  • 101
  • 1
  • 2
  • 10
  • 2
    `.git` is a folder and not a file. maybe this will help: https://stackoverflow.com/a/57866165/1169798 – Sirko Dec 12 '22 at 08:09

1 Answers1

0

As in the comment section, .git is a folder, not a file, so you will need to use rmSync, e.g. to remove the folder recursively and force delete, you can try:

fs.rmSync(dir, { recursive: true, force: true });

You can read more about the options here: https://nodejs.org/docs/latest/api/fs.html#fsrmsyncpath-options

DreamBold
  • 2,727
  • 1
  • 9
  • 24