# Programatically delete instances of nodes

**URL:** <https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791>\
**Category:** General\
**Created:** [29 July 2019 11:54 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791 "2019-07-29T11:54:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [29 July 2019 11:54 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/1 "2019-07-29T11:54:05Z")

</div>

I'm sure I'm missing something simple here but I'm not seeing it. I can easily use my BigTimer Node to trigger something (like an Alexa Notify Node) later this week - but this is a one-off event. I want to have Node-Red-contrib-bigtimer Tell Alexa to queue a notification once only... Ater it has sent off the notification to Alexa later in the week, I want Node-Red to delete that particular BigTimer, the Notify node and the wire that joins them - as if they'd never been put in flow in thw first place. There might be othe similar one-offs in that flow for different times.

Ideas? Am I missing something trivial or is this a monster to do automatically, programatically without me being there?

---

<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:** [29 July 2019 12:28 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/2 "2019-07-29T12:28:16Z")

</div>

I'd thought it'd be possible to launch say a python script that would kill your Node-RED process, edit your flow.json to remove the nodes and restart Node-RED but it'd be quite a bit of work and pain to debug 🙂

[edit]It would be an easier task if you just put the BigTimer & Alexa nodes in a seperate tab with link to main tab and get your python code to to just change Flow disable from false to true

Much easier to debug

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

What's your actual use case that requires your BigTimer node to be removed from the flow once its triggered.

---

<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:** [29 July 2019 13:02 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/3 "2019-07-29T13:02:55Z")

</div>

Personally, I would have thought it better to have a job scheduler with an option to have one-off jobs that then auto-delete themselves.

Perhaps a request to the following thread might end up with an additional feature:

> [@UPDATE: V1.2.1 node-red-contrib-cron-plus scheduler (incl solar events and Timezone support)](https://discourse.nodered.org/t/new-a-flexible-scheduler-node-incl-timezone-support-node-red-contrib-cron-plus/13686):
>
> Hi all, something I am working on. Would appreciate feedback/suggestions/criticisms please. Features... Timezone support human readable descriptions of cron Supports standard CRON + year Some pictures for now (details later). Help tab... In action... In action... Timezone... [image] Timezone... [image]

Trying to edit the flow using a flow seems like a recipe for a disaster. Trying to understand that flow later on could be a nightmare.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [29 July 2019 13:35 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/4 "2019-07-29T13:35:33Z")

</div>

Would probably be easier to simply have BigTimer (as I wrote BigTimer) delete itself once it had run once... I thought of having BigTimer or Inject delete their own Payload once they'd run, but that is no good for "Alexa NotifyMr" node - it just gripes about no notification.

---

<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:** [29 July 2019 13:36 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/5 "2019-07-29T13:36:48Z")

</div>

Basic POC

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

```auto
[{"id":"c1222112.cd919","type":"inject","z":"a208955f.d140e8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":140,"wires":[["4a1838df.ea67c8"]]},{"id":"592048ea.eba7a8","type":"debug","z":"a208955f.d140e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":810,"y":120,"wires":[]},{"id":"4a1838df.ea67c8","type":"exec","z":"a208955f.d140e8","command":"bash /home/pi/disable.sh","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":450,"y":140,"wires":[["592048ea.eba7a8"],[],[]]}]

```

disable.sh

```auto
#ps aux
#/usr/bin/lxterm &disown
sed -i -e 's/Flow 2","disabled":false/Flow 2","disabled":true/g' /home/pi/.node-red/flows_pi420191007.json
# >/home/pi/.node-red/test.txt
sudo pkill -f node-red
cd /home/pi/.node-red
node-red

```

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [29 July 2019 16:47 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/6 "2019-07-29T16:47:57Z")

</div>

> [@TotallyInformation](#):
>
> Trying to edit the flow using a flow seems like a recipe for a disaster.

I dimly remember dire warnings against self-modifying code...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 July 2019 19:15 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/7 "2019-07-29T19:15:38Z")

</div>

@TotallyInformation as it happens, I have added the ability to "add", "remove", "start", "stop" & "list" schedules as a topic input into the node. Essentially that makes this possible

e.g. Something (dashboard/api/mqtt) causes a request for a one time event at "09:00:00" (

1. inject topic `"add"` with payload `{"name":"9am", "expression":"0 0 9 * * *"`}
2. when the node triggers with a topic of "9am" - this can then be used to `"remove"` the job.

[update]...  
actually, sod it, I decided to also add a `.limit` property so it will be even easier. 🙂

```auto
//inject this into a cron-plus node...
msg.topic = "add";
msg.payload = {"name":"9am", "expression":"0 0 9 * * *", limit:1};

```

I'll be posting an update in a few days.

---

<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:** [29 July 2019 19:41 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/8 "2019-07-29T19:41:18Z")

</div>

Hmm now that payload is a growing object. You may as well not use topic and put the command into the payload as well 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 July 2019 20:02 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/9 "2019-07-29T20:02:03Z")

</div>

yeah, its one of those things. Do ya, dont ya. 🤔

What would be your advice / recommendation on sending control type inputs to a node?

_(sorry to hijack the thread)_

---

<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:** [29 July 2019 20:07 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/10 "2019-07-29T20:07:39Z")

</div>

Well, I've not the expertise of Dave but my preference would be to keep the topic clear as it can be useful to pass through as a "tag" or "category" through a flow.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [30 July 2019 05:34 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/11 "2019-07-30T05:34:05Z")

</div>

This one certainly sems to have wings. My current solution unfortunately needs both the name and payload updating in order to see the subject of a replacement scheduled event in the flow before it sets up. The ideal would me to set up a message in this case to the Alexa NotifyMe node, to be actually sent to the node, say, next Friday lunchtime or at a specific time/day in a couple of weeks. Alexa would then turn green from then on until I actually go to Alexa and ask for notifications. I can easily use BigTimer or the Inject Node to set up repeating outputs every day or week or month et at specific times, but a one-off event not as easily. I briefly looked at node-red-contrib-calendar-trigger but that needs a database and hasn't been updated in years.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [30 July 2019 08:14 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/12 "2019-07-30T08:14:48Z")

</div>

> [@scargill](#):
>
> I briefly looked at node-red-contrib-calendar-trigger but that needs a database and hasn't been updated in years.

I was "castigated" for mentioning its update cycle previously 😊  
But it certainly does what it says on the tin, and it doesn't _need_ a database, but it could do with some enhanced functionality. It is quite basic.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [30 July 2019 11:57 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/13 "2019-07-30T11:57:47Z")

</div>

Ok I'll take another look just in case.

---

<div class="post-metadata">

**Author:** ![fragolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fragolinux/32/1137_2.png) [@fragolinux](https://discourse.nodered.org/u/fragolinux)\
**Post date:** [5 August 2019 13:09 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/14 "2019-08-05T13:09:32Z")

</div>

@scargill Pete, what about using a command line json editor?  
and thinking now, we can use this to modify json files in your script, too...  
info: [https://stackoverflow.com/questions/43292243/how-to-modify-a-keys-value-in-a-json-file-from-command-line](https://stackoverflow.com/questions/43292243/how-to-modify-a-keys-value-in-a-json-file-from-command-line)

---

<div class="post-metadata">

**Author:** ![fragolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fragolinux/32/1137_2.png) [@fragolinux](https://discourse.nodered.org/u/fragolinux)\
**Post date:** [14 August 2019 09:32 UTC](https://discourse.nodered.org/t/programatically-delete-instances-of-nodes/13791/15 "2019-08-14T09:32:41Z")

</div>

Pete, what about a "flow" boolean variable set to TRUE, put in AND with the input to bigtimer... after bigtimer started, put it to FALSE, and all of that will be disabled... no need to remove anything from flow, and all there if you need that again
