Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting file in Node.js

I am using node in a Windows environment. When I use fs.unlinkSync(fileName), it seems to work. After the unlinkSync statement is executed, if I do a fs.existsSync(filename) it returns false indicating the file does not exists, but when I go to the physical drive I can still see the file.

At this point in time if I try and delete the file manually, it throws Access denied. However, the file is automatically removed from the file system only when I stop the executing node script file.

Is this the expected behavior?

like image 356
user1961100 Avatar asked Dec 27 '13 07:12

user1961100


People also ask

How do you delete a file in node?

js fs-extra remove() Function. the remove() function deletes the given file or directory. All the files inside a directory are deleted.

Which methods can be used to delete a file in node JS?

In Node. js, you can use the fs. unlink() method provided by the built-in fs module to delete a file from the local file system.

Can I delete a .js file?

You can't delete a file when running JavaScript from the browser, but you can do it when running JavaScript from a server environment like NodeJS. When you need to delete a file using NodeJS, You can use the fs. unlink() or fs.


1 Answers

If the file has been opened and not closed within your NodeJS code, you'll encounter the behavior you're experiencing. It's behaving as expected, on Windows.

Take this code for example:

var fs = require('fs');

var filename = "D:\\temp\\temp.zip";
var tempFile = fs.openSync(filename, 'r');
// try commenting out the following line to see the different behavior
fs.closeSync(tempFile);

fs.unlinkSync(filename);

If the code uses openSync and then closeSync on the file, the file is immediately deleted when unlinkSync is called. If however, you were to remove the closeSync call, you'll find that the file is deleted only when the NodeJS process cleanly exits. This is just one example of a way to cause this problem to occur.

If you're using a third party library that is processing files, etc., it's possible the code is not properly closing the file handles/descriptors and you would also encounter this issue (for the same reason).

FYI: The file will appear to be deleted immediately if you test this code on a Linux based operating system. It's a difference in behavior between the operating systems and the way files are deleted.

Details

When NodeJS on Windows deletes a file, it's actually not calling an API to directly delete a file. Instead, it's using a low level function called ZwSetInformation (reference). With that function, it's setting a field called DeleteFile to TRUE for the specified file (handle). That's used so that when all file handles are closed for that file, it will be automatically deleted. Files in NodeJS are opened with the access mode of FILE_SHARE_DELETE so that they can be deleted properly by use of the other function.

like image 149
WiredPrairie Avatar answered Sep 29 '22 10:09

WiredPrairie