# Problem Delete File on Windows

**URL:** <https://discourse.nodered.org/t/problem-delete-file-on-windows/16400>\
**Category:** General\
**Created:** [7 October 2019 03:58 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400 "2019-10-07T03:58:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [7 October 2019 03:58 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/1 "2019-10-07T03:58:55Z")

</div>

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'

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/38e2eceda2869a45a8bfaebd8110cb44858f5c80.png)

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 October 2019 05:41 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/2 "2019-10-07T05:41:19Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 October 2019 06:10 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/3 "2019-10-07T06:10:21Z")

</div>

> [@mrizkywiguna](#):
>
> fs-ops-delete

> **[GitHub - DeanCording/node-red-contrib-fs-ops: A Node Red node for performing...](https://github.com/DeanCording/node-red-contrib-fs-ops)**
>
> A Node Red node for performing file system operations - GitHub - DeanCording/node-red-contrib-fs-ops: A Node Red node for performing file system operations

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.

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [7 October 2019 06:23 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/4 "2019-10-07T06:23:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 October 2019 07:04 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/5 "2019-10-07T07:04:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [7 October 2019 07:52 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/6 "2019-10-07T07:52:41Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6d821d698feb2ea7fc921fb7c06a1c180cdffd3e.png)

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [7 October 2019 07:53 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/7 "2019-10-07T07:53:32Z")

</div>

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 😃

```auto
[
    {
        "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": []
    }
]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a8c90023a0b6e2208cd5b99464752cbff8a378eb.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [7 October 2019 08:09 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/8 "2019-10-07T08:09:40Z")

</div>

Try using this - seems to work for me

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c834968ec15e9192626bec81f646aa8917189674.png)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [7 October 2019 08:43 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/9 "2019-10-07T08:43:25Z")

</div>

> [@cymplecy](#):
>
> [{"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'"

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 October 2019 09:48 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/10 "2019-10-07T09:48:18Z")

</div>

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](https://github.com/npm/npm/issues/18287))

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [7 October 2019 09:55 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/11 "2019-10-07T09:55:20Z")

</div>

404 on that link

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 October 2019 10:27 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/12 "2019-10-07T10:27:24Z")

</div>

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 🥴

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [7 October 2019 10:38 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/13 "2019-10-07T10:38:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [7 October 2019 13:39 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/14 "2019-10-07T13:39:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 October 2019 15:55 UTC](https://discourse.nodered.org/t/problem-delete-file-on-windows/16400/15 "2019-10-07T15:55:00Z")

</div>

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.
