How to delete a file using JavaScript

In test automation projects, if you need to delete a 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


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts