How to delete file using JavaScript

In test automation projects, if you need to delete file using javascript, you can leverage the ‘fs’ node package. These are very important for any Javascript-based test automation project. Let’s see how to delete a file using javascript.

In order to use this fs module, you need to call it in your code like below.

var fs = require('fs');
How to delete a file using javascript – complete example code
  async deleteFile(fileAbsPath){
    if (fs.existsSync(fileAbsPath)) {
      fs.unlink(fileAbsPath, function (err) {
        if (err) throw err;
        console.log('File deleted!');
      });
    } else {
      console.log('file not present')
    }

  }

You may also read

Related Posts