Pramod
Pramod

Reputation: 434

Unable to delete file using fs.removeSync()

I am using angular 6. I want to delete multiple files from backend folder for that, I am using fs.removeSync() but it gives below exception for me. can someone help me?

"UnhandledPromiseRejectionWarning: TypeError: fs.removeSync is not a function "

My Code:

fs.removeSync('/NodeWorkspace/uploads/output/*.csv');

Upvotes: 3

Views: 10746

Answers (3)

AB HAT
AB HAT

Reputation: 87

fs.removeSync(path) is a function of fs-extra library which is a wrapper over fs provided by nodejs.

Upvotes: 4

Mohammad Rajabloo
Mohammad Rajabloo

Reputation: 2943

Based on node.js documentation removeSync function not exist. For delete file use unlinkSync function like this:

fs.unlinkSync(path)

But I don't think that work for multiple files, you can use glob package:

var glob = require("glob")

// options is optional
glob("/NodeWorkspace/uploads/output/*.csv", options, function (er, files) {
    for (const file of files) {
         fs.unlinkSync(file);
    }
})

Note: Remember for delete directory use fs.rmdir();

Upvotes: 11

Sagar More
Sagar More

Reputation: 476

Try using fs.unlinkSync(path).

Upvotes: 1

Related Questions