William Tang
William Tang

Reputation: 53

How to delete a file permanently with Node.js?

I need to create a function in NodeJS that permanently deletes temporary files (instead of just deleting the normal way and storing the deleted files in the trash). In fact, my program will create a lot of temporary files and eventually the server's hard drive will get full very fast.

Upvotes: 1

Views: 2669

Answers (2)

Paul Losev
Paul Losev

Reputation: 1089

Using unlink (with callback) /unlinkSync in fs module.

const fs = require('fs')

fs.unlinkSync(__dirname + 'yourfile.name')

Upvotes: 0

IceMetalPunk
IceMetalPunk

Reputation: 5556

fs.unlink(path, callback) will delete a file. As with all the fs methods, there's also the synchronous version, fs.unlinkSync(path).

https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback

Upvotes: 3

Related Questions