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

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.