hussain hussain - 3 months ago 14
Javascript Question

fs file deletion from the directory using nodejs?

I am deleting file when i receive event

stopRecording
from client that is working as expected file is being deleted from directory, My question here is when i look into directory using windows(webstorm editor) filename is still visible there but when i click on filename it got removed from directory.I am not sure what i should do here because functionality is working its just matter of filename visibility until you click its there. what will be behavior on server and how can i resolve this problem ?

logRecording.js

deleteFile:function (filename) {
var path = './app/records/templogs';
var file = path + '/'+ filename;
console.log('filename in delete', filename);
fs.unlink(file);
}


socketIo.js

socket.on('stopRecording',function (filename) {
console.log('stop recording data',filename);
logsRecording.deleteFile(filename);
});

mmm mmm
Answer

Webstorm sometimes needs some time to renew the file system structure/it's files. Don't worry, the file is deleted, it just might take some time to show up.

It's not a problem with your code.

SIDENOTE

You may experience the same with newly created files, sometimes they won't show up until you manually refresh the project structure.

If you are very curious, try the following - but remember it will throw an error every time, so don't use it in production code:

deleteFile:function (filename) {
    var path = './app/records/templogs';
    var file = path + '/'+ filename;
    console.log('filename in delete', filename);
    fs.unlink(file);
    fs.statSync(file);
}
Comments