Search code examples
javascriptnode.jswebstormfs

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 deleted from directory.

My question here is when I look into directory using Windows (WebStorm IDE) file name is still visible there but when I click on file name it got removed from directory.

I am not sure what I should do here because functionality is working it's 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);
        });

Solution

  • 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);
    }