Problem Delete File on Windows

Hi Guys,

I have problem to delete the file that i created with node-red on Windows OS. I need delete the file scheduled to write new file again after deleted. Here is the error message:

Error: EPERM: operation not permitted, unlink 'D:\test\text.csv'

[{"id":"2f8f3817.a125d","type":"inject","z":"b405e48.6a27118","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":247.5,"y":312,"wires":[["eb5ee9f8.6484c8"]]},{"id":"9cfc3a5e.a8c9f8","type":"file","z":"b405e48.6a27118","name":"","filename":"D:\\test\\text.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":721.5,"y":231,"wires":[[]]},{"id":"eb5ee9f8.6484c8","type":"fs-ops-delete","z":"b405e48.6a27118","name":"","path":"D:\\test","pathType":"str","filename":"text.csv","filenameType":"str","x":712.5,"y":312,"wires":[[]]},{"id":"fb1d53fc.de6eb","type":"inject","z":"b405e48.6a27118","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":261,"y":231,"wires":[["9cfc3a5e.a8c9f8"]]}]

Thanks

I'm not sure what node you are using to delete the file, but can't you just use a cors file out node set to overwrite or delete the file ?

You could try doubling up the back-slashes in case the node author hasn't handled Windows file strings correctly.

Ah, actually, thinking about it, it is more likely that the file is still open and therefore cannot be deleted yet.

Because I'm using "Append to File" action to always get the data, its make the file cannot deleted.

My solutions is after inject the fs-ops-delete, restart the flows. The file can deleted.

If i using Linux its no problem, i can use exec node and command node-red-stop / sudo systemctl stop nodered. But how to do that on Windows OS?

Do you mean that if append is specified then the file is kept open between writes? That surprises me, though I have not tested it.

That suprised me as well but looks like node defaults to holding file open for performance reasons but can be switched to closing after each write by setting msg.filename in a change node before it

Yes Colin it's like my file under process with node-red , so i can't delete the file using fs-ops-delete/file delete node.

Here is my tricky solutions, maybe someone have a better way :smiley:

[
    {
        "id": "9cfc3a5e.a8c9f8",
        "type": "file",
        "z": "b405e48.6a27118",
        "name": "",
        "filename": "D:\\test\\text.csv",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1003.5,
        "y": 236,
        "wires": [
            []
        ]
    },
    {
        "id": "fb1d53fc.de6eb",
        "type": "inject",
        "z": "b405e48.6a27118",
        "name": "",
        "topic": "",
        "payload": "A",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 251,
        "y": 231,
        "wires": [
            [
                "e1985741.2305f"
            ]
        ]
    },
    {
        "id": "e0d6fdf2.a8cbb8",
        "type": "inject",
        "z": "b405e48.6a27118",
        "name": "",
        "topic": "",
        "payload": "---------------------------------------------",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 244,
        "y": 334,
        "wires": [
            [
                "eaaaca92.f2e5d"
            ]
        ]
    },
    {
        "id": "e1985741.2305f",
        "type": "csv",
        "z": "b405e48.6a27118",
        "name": "",
        "sep": ",",
        "hdrin": "",
        "hdrout": "",
        "multi": "one",
        "ret": "\\n",
        "temp": "A",
        "skip": "0",
        "strings": true,
        "x": 606.5,
        "y": 237,
        "wires": [
            [
                "9cfc3a5e.a8c9f8"
            ]
        ]
    },
    {
        "id": "eaaaca92.f2e5d",
        "type": "file",
        "z": "b405e48.6a27118",
        "name": "",
        "filename": "D:\\test\\text.csv",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 658,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "9db58675.e5915",
        "type": "comment",
        "z": "b405e48.6a27118",
        "name": "write",
        "info": "",
        "x": 210.5,
        "y": 183,
        "wires": []
    },
    {
        "id": "90ea8382.505d78",
        "type": "comment",
        "z": "b405e48.6a27118",
        "name": "delete",
        "info": "",
        "x": 224,
        "y": 293,
        "wires": []
    }
]

Try using this - seems to work for me

image

[{"id":"771b0325.d7110c","type":"inject","z":"53d3fd8f.2bbaa4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":236,"y":748,"wires":[["891eb9d8.1163f8"]]},{"id":"647641c7.6276","type":"file","z":"53d3fd8f.2bbaa4","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"delete","encoding":"none","x":622,"y":748,"wires":[[]]},{"id":"441be72c.7ae0d8","type":"comment","z":"53d3fd8f.2bbaa4","name":"delete","info":"","x":224.5,"y":695,"wires":[]},{"id":"891eb9d8.1163f8","type":"change","z":"53d3fd8f.2bbaa4","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"d:\\test\\text.csv","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":442,"y":748,"wires":[["647641c7.6276"]]}]

Hi Cymplecy,

Still got error,

"failed to delete file: Error: EPERM: operation not permitted, unlink 'd:\test\text.csv'"

The delete after an append works fine for me on a Pi and in OSX.

this might be an npm issue (https://github.com/npm/npm/issues/18287)

what are the versions of
node-red
node.js
npm
windows

404 on that link

Fixed it. Note to self: never put links in parentheses if you don't make sure you used shift-0 instead of just 0 for the closing :woozy_face:

2 Likes

I don't think that old issue relates to OP's problem

Hi @zenofmud,

node-red : 0.20.8 updated to version 1 still get the error
nodejs : 10.16.3
npm : 6.9.0
windows 7 64bit

The file should be closed after writes have finished otherwise Windows NTFS will retain a lock on it which prevents deletion.

node.js fs will, I believe, keep the file open if used as a stream - which is doubtless a lot more efficient.

Not sure how the file node works but possibly it needs tweaking. You could also use a utility to view the file locks to see when/if it is being released.