# Deleting a bunch of files with Exec Node

**URL:** https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684
**Category:** General
**Created:** [18 April 2023 09:05 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684 "2023-04-18T09:05:52Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 09:05 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/1 "2023-04-18T09:05:53Z")

</div>

Hi!

I want to delete a bunch of files using the exec node.  
When I try to start the job with the payload

```auto
rm /mnt/dietpi_userdata/File-to-delete*

```

the exec node tells me:

> rm: cannot remove '/mnt/dietpi\_userdata/File-to-delete\*': No such file or directory

What am I doing wrong? A single file in this directory is possible to delete, Node-RED has sufficient rights in this directory...

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [18 April 2023 09:13 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/2 "2023-04-18T09:13:03Z")

</div>

I think the issue is the `*` - it doesn't get expanded the way it would when you run the command in a local shell normally. So it is looking for a file called literally `File-to-delete*`

---

<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: [18 April 2023 09:13 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/3 "2023-04-18T09:13:50Z")

</div>

I think it has something to do with the file spec being put in quotes?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 April 2023 09:51 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/4 "2023-04-18T09:51:04Z")

</div>

I don't see any problem using exec to **rm /home/pi/junk/delete** \*, suggesting that there is no mistake in interpretation of the asterisk.

And if there are in fact no files to delete the error message in debug is  
`rm: cannot remove '/home/pi/junk/delete*': No such file or directory `  
ie the single quotes are an artifact of the error message (of course single quotes around the command would break it).

So what do your actual filenames look like?  
Does it make a difference if you try to delete multiple files in a local directory rather than on a mounted filesystem?  
Does it work if you change the command to this?

```auto
for f in /mnt/dietpi_userdata/File-to-delete*; do rm $f; done

```

---

<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: [18 April 2023 10:06 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/5 "2023-04-18T10:06:59Z")

</div>

How did you install node-red? Is this a Docker install for example?

How is that folder mounted?

In the exec node try running `ls -l /mnt/dietpi_userdata` to check whether you can see the directory.

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 10:10 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/6 "2023-04-18T10:10:19Z")

</div>

The filenames are like

> Backup\_Config\_PROTO\_b23025\_2023-4-18\_10\_53\_09\_MAIN.json

Using your command ends up with

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

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 10:18 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/7 "2023-04-18T10:18:40Z")

</div>

@Colin: it's a local install, no docker installation.

I can use the directory, and also delete ONE file if I write in the command the whole filename without an asterisk. So, like @knolleary said, it seems to not getting expanded like in the local shell...

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 10:24 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/8 "2023-04-18T10:24:59Z")

</div>

> [@Colin](#):
>
> In the exec node try running `ls -l /mnt/dietpi_userdata` to check whether you can see the directory.

Yes, I can see all of the files in the directory.

If I try to list via `ls -l /mnt/dietpi_userdata/Back*` I get the same error as before.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 April 2023 10:31 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/9 "2023-04-18T10:31:38Z")

</div>

Can you try `ls -l "/mnt/dietpi_userdata/Back*"`

I don't expect it to be any different.

> [@jbudd](#):
>
> Does it make a difference if you try to delete multiple files in a local directory rather than on a mounted filesystem?

> [@Colin](#):
>
> How is that folder mounted?

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 10:57 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/10 "2023-04-18T10:57:07Z")

</div>

> [@jbudd](#):
>
> Can you try `ls -l "/mnt/dietpi_userdata/Back*"`
> 
> I don't expect it to be any different.

You're right - no difference:

`ls: cannot access '/mnt/dietpi_userdata/bb_config/Back*': No such file or directory`

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 April 2023 11:00 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/11 "2023-04-18T11:00:21Z")

</div>

I presume the bb\_config directory does exist, you just left it out of previous replies here! 😃

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 11:08 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/12 "2023-04-18T11:08:39Z")

</div>

> [@Colin](#):
>
> How is that folder mounted?

I'm not sure how to find out... it's the default user directory in DietPi. Since I am far away from a linux expert (normally using a mac for every-day-use), I have no idea, where to look. If it's important to know - please give me a hint...

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 11:10 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/13 "2023-04-18T11:10:36Z")

</div>

> [@jbudd](#):
>
> I presume the bb\_config directory does exist, you just left it out of previous replies here! 😃

Yes, you're right!  
The directory does exist, and also the files! 🙂  
And: Node-RED Exec can have access to the files and also delete one by one - but not, if I try to delete all of my "Backup\_"-Files.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 April 2023 11:30 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/14 "2023-04-18T11:30:39Z")

</div>

Yes I understand your exec can delete them one by one but not en masse.

It is puzzling because I find that the exec node does successfully pass the asterisk to bash on the target machine. ie exec can delete using asterisk.  
My setup is not identical to yours: my Linux machine runs RpiOS and I don't have any mounted filesystems. Nevertheless, it ought to work for you.

A massive overkill exec command to try:  
This should find all "normal" files (ie not directories) which are below /mnt/dietpi\_userdata and whose names start with "delete" (change as appropriate)  
Since the filename pattern is enclosed in single quotes, it should be protected until find interprets it.  
Each file it finds will be deleted.  
The files removed will be listed in the exec node output.

```auto
find /mnt/dietpi_userdata -name 'delete*' -type f -print0 | xargs -0 rm -v

```

Use with caution - a typo in the path could remove files you don't want removed.

You can also try writing a shell script on the dietpi mahine, containing the same as your exec command, then exec that script. By moving the asterisk expansion onto the target machine it removes exec as a possible source of error

---

<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: [18 April 2023 11:33 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/15 "2023-04-18T11:33:14Z")

</div>

try setting `Command` in the `exec` node to 'rm'  
then in `+Append` enter `/mnt/dietpi_userdata/File-to-delete*.*`

Notice the `.*` at the end

works fine for me. If no files exist the exec node returns

> rm: cannot remove '/home/pi/atestdelete/file0\*.\*': No such file or directory

Note that is from my test

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 April 2023 12:24 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/16 "2023-04-18T12:24:27Z")

</div>

> [@zenofmud](#):
>
> try setting `Command` in the `exec` node to 'rm'  
> then in `+Append` enter `/mnt/dietpi_userdata/File-to-delete*.*`
> 
> Notice the `.*` at the end

Thanks Paul!  
This works also for me - and it also does the job **without** the `.*` at the end. Seems to be something special with the `+Append` function.

Now I can carry on! 😀

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 May 2023 12:25 UTC](https://discourse.nodered.org/t/deleting-a-bunch-of-files-with-exec-node/77684/17 "2023-05-02T12:25:26Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
