# \[Solved\] Restarting flows programmatically

**URL:** <https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341>\
**Category:** Feature Requests\
**Created:** [23 March 2019 03:20 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341 "2019-03-23T03:20:21Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [23 March 2019 03:20 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/1 "2019-03-23T03:20:21Z")

</div>

Since v0.20, the deploy menu has a new `Restart Flows` option. Would it be possible to provide a way to invoke this programmatically from within a flow? For example, in cases where message properties have been used to override node parameters set when a node was deployed (not encouraged, I realize, but sometimes a good idea), it could be useful to be able to recover the initial state of the flow.

---

<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:** [23 March 2019 13:48 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/2 "2019-03-23T13:48:54Z")

</div>

Looks like it simply POSTs to the API function `flows`

```auto
        $.ajax({
            url:"flows",
            type: "POST",
            headers: {
                "Node-RED-Deployment-Type":"reload"
            }
        }).done(function(data,textStatus,xhr) {
            if (deployWasEnabled) {
                $("#btn-deploy").removeClass("disabled");
            }
            RED.notify('<p>'+RED._("deploy.successfulRestart")+'</p>',"success");
        }).fail(function(xhr,textStatus,err) {
            if (deployWasEnabled) {
                $("#btn-deploy").removeClass("disabled");
            }
            if (xhr.status === 401) {
                RED.notify(RED._("deploy.deployFailed",{message:RED._("user.notAuthorized")}),"error");
            } else if (xhr.status === 409) {
                resolveConflict(nns, true);
            } else if (xhr.responseText) {
                RED.notify(RED._("deploy.deployFailed",{message:xhr.responseText}),"error");
            } else {
                RED.notify(RED._("deploy.deployFailed",{message:RED._("deploy.errors.noResponse")}),"error");
            }
        }).always(function() {
            deployInflight = false;
            var delta = Math.max(0,300-(Date.now()-startTime));
            setTimeout(function() {
                $(".deploy-button-content").css('opacity',1);
                $(".deploy-button-spinner").hide();
                $("#header-shade").hide();
                $("#editor-shade").hide();
                $("#palette-shade").hide();
                $("#sidebar-shade").hide();
            },delta);
        });

```

You can do that yourself:

[https://nodered.org/docs/api/admin/methods/post/flows/](https://nodered.org/docs/api/admin/methods/post/flows/)

---

<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:** [23 March 2019 18:56 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/3 "2019-03-23T18:56:40Z")

</div>

Brilliant! Thanks a lot. I suspected/hoped that this was available in the Admin API, but I didn't know where to look in the docs. It took me a while to work out the DIY part.

---

<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:** [23 March 2019 19:01 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/4 "2019-03-23T19:01:27Z")

</div>

No problem. Just note that I had some problems with jQuery's POST function so I used a raw `var request = new XMLHttpRequest()` in uibuilder. Though that may be why Nick used `$.ajax` here rather than `$.post()`. It is a known bug that triggers a CORS error.

In fact the raw query isn't actually any harder to use than jQuery except that you don't get the promises type interface.

---

<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:** [23 March 2019 19:06 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/5 "2019-03-23T19:06:41Z")

</div>

Noted. I also had some issues with POST, but it seems to work ok in the particular case I need. If it does go sideways, I'll know what to do.

---

<div class="post-metadata">

**Author:** ![ampledata](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ampledata](https://discourse.nodered.org/u/ampledata)\
**Post date:** [6 June 2019 01:56 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/6 "2019-06-06T01:56:39Z")

</div>

Hey @TotallyInformation or @drmibell, I'm trying to implement this now, but getting a 400 when I try to actually send the reload to the /flows endpoint. Would either of you have an example working flow you could share?

---

<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:** [6 June 2019 02:14 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/7 "2019-06-06T02:14:38Z")

</div>

Sorry, but I can't recover my work on this. I ended by solving my original problem in a completely different way. Perhaps @TotallyInformation can help.

---

<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:** [6 June 2019 10:19 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/8 "2019-06-06T10:19:25Z")

</div>

Sorry, my comments were about using jQuery's post function that can have some issues. Not actually needed to do programmatic flow restarts myself.

---

<div class="post-metadata">

**Author:** ![LiorBird](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@LiorBird](https://discourse.nodered.org/u/LiorBird)\
**Post date:** [13 February 2020 15:51 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/9 "2020-02-13T15:51:41Z")

</div>

Anyone ever managed to implement that? I have to do the same exact thing, but not sure how to do it exactly. Thanks!

---

<div class="post-metadata">

**Author:** ![mhechthz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mhechthz/32/20144_2.png) [@mhechthz](https://discourse.nodered.org/u/mhechthz)\
**Post date:** [14 May 2020 14:24 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/10 "2020-05-14T14:24:24Z")

</div>

Hmmm ... I tried to do as proposed but get error 401. I do this:

`[{"id":"f60dd315.81c64","type":"function","z":"fe0113d0.6b338","name":"reload flows","func":"//\n// url:\"flows\",\n// type: \"POST\",\n// headers: {\n// \"Node-RED-Deployment-Type\":\"reload\"\n// }\n\nmsg.payload = {headers:{\"Node-RED-Deployment-Type\":\"reload\"}};\n\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":660,"wires":[["e83f1e8f.5db03"]]},{"id":"e83f1e8f.5db03","type":"http request","z":"fe0113d0.6b338","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"localhost:1880/flows","tls":"","persist":false,"proxy":"","authType":"","x":680,"y":660,"wires":[["a409437b.1c6a3"]]},{"id":"2021734a.df7a4c","type":"inject","z":"fe0113d0.6b338","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":660,"wires":[["f60dd315.81c64"]]},{"id":"a409437b.1c6a3","type":"debug","z":"fe0113d0.6b338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":920,"y":660,"wires":[]}]`

---

<div class="post-metadata">

**Author:** ![droibles](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droibles/32/52370_2.png) [@droibles](https://discourse.nodered.org/u/droibles)\
**Post date:** [9 December 2021 21:55 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/11 "2021-12-09T21:55:09Z")

</div>

I finally managed to do this and can programatically restart my flows from within Node Red! Took me a while to work out.

If you are still interested I'll type it up, but I don't want to do it if it's not required any more.

---

<div class="post-metadata">

**Author:** ![mikey](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mikey](https://discourse.nodered.org/u/mikey)\
**Post date:** [3 February 2022 11:26 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/12 "2022-02-03T11:26:28Z")

</div>

i could use your flow to finish my program if you dont mind sharing @droibles

---

<div class="post-metadata">

**Author:** ![droibles](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droibles/32/52370_2.png) [@droibles](https://discourse.nodered.org/u/droibles)\
**Post date:** [3 February 2022 15:05 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/13 "2022-02-03T15:05:29Z")

</div>

Happily. Just at work now, I'll type it up later. 👍

---

<div class="post-metadata">

**Author:** ![mikey](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mikey](https://discourse.nodered.org/u/mikey)\
**Post date:** [3 February 2022 15:20 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/14 "2022-02-03T15:20:12Z")

</div>

thank you!!!

---

<div class="post-metadata">

**Author:** ![droibles](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droibles/32/52370_2.png) [@droibles](https://discourse.nodered.org/u/droibles)\
**Post date:** [4 February 2022 19:16 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/15 "2022-02-04T19:16:53Z")

</div>

Hi, sorry I didn't get around to this last night.

Here is my flow:

```auto
[{"id":"880a338331a21a4d","type":"function","z":"ea6852638fc51d01","name":"Prepare reload flows header","func":"\nvar secret_token = msg.payload.access_token\n\nmsg.payload = {}\nmsg.headers = {}\nmsg.method = \"POST\"\nmsg.url = \"http://nodered.test.com:1880/flows\" ;\n\nmsg.headers[\"Authorization\"] = \"Bearer \" + secret_token\nmsg.headers[\"content-type\"] = \"application/json\"\nmsg.headers[\"Node-RED-Deployment-Type\"] = \"reload\"\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":460,"wires":[["06bbbe481f4e9155","fb0f73fd1309d014"]]},{"id":"06bbbe481f4e9155","type":"http request","z":"ea6852638fc51d01","name":"Request reload flows","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1120,"y":520,"wires":[["22e2281626f78481"]]},{"id":"b4d34199e00e0e4b","type":"inject","z":"ea6852638fc51d01","name":"Hourly","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3600","crontab":"","once":false,"onceDelay":"20","topic":"","payloadType":"date","x":120,"y":400,"wires":[["bdeb53f7f3a9080e"]]},{"id":"22e2281626f78481","type":"debug","z":"ea6852638fc51d01","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":520,"wires":[]},{"id":"bdeb53f7f3a9080e","type":"function","z":"ea6852638fc51d01","name":"Pepare auth token header","func":"msg.headers = {}\nmsg.method = \"POST\"\nmsg.url = \"http://nodered.test.com:1880/auth/token\" ;\nmsg.headers[\"content-type\"] = \"application/json\"\n\n\nmsg.payload = {\n\"client_id\":\"node-red-admin\",\n\"grant_type\":\"password\",\n\"scope\":\"*\",\n\"username\":\"your_username\",\n\"password\":\"your_password\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":400,"wires":[["e18a15e1dafaa148"]]},{"id":"e18a15e1dafaa148","type":"http request","z":"ea6852638fc51d01","name":"Request auth token","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":570,"y":400,"wires":[["880a338331a21a4d","a524d01d1ddfb876"]]},{"id":"a524d01d1ddfb876","type":"debug","z":"ea6852638fc51d01","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":400,"wires":[]},{"id":"fb0f73fd1309d014","type":"debug","z":"ea6852638fc51d01","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":460,"wires":[]}]

```

I've replaced my NodeRed URL in both of the function nodes `msg.url` variables with "[nodered.test.com](http://nodered.test.com)" so this will need to be changed to your NodeRed URL.

Also the your\_username and your\_password in the first function node will need to be replaced with your credentials.

The first function/request gets the auth token which is then used by the second request to restart the flows.

This might not be the most elegant piece of programming but I am no Javascript expert. I'm sure there are better ways of storing the username and password more securely for example.

It does the job for me though. I needed it because somewhere in my setup (I have 24 flow tabs with some fairly complex flows) I have a rogue flow that fails sometimes and pushes my NodeRed process to 100%. So you'll see I actually trigger the restart every hour.

I hope it helps you.

---

<div class="post-metadata">

**Author:** ![mikey](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mikey](https://discourse.nodered.org/u/mikey)\
**Post date:** [4 February 2022 19:21 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/16 "2022-02-04T19:21:32Z")

</div>

i appreciate it i needed a check down to restart a failing flow...not the best method or fix but if it buys me some time to straighten it out it exactly what i need. Thank you !! i appreciate your flow

---

<div class="post-metadata">

**Author:** ![droibles](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droibles/32/52370_2.png) [@droibles](https://discourse.nodered.org/u/droibles)\
**Post date:** [5 February 2022 10:18 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/17 "2022-02-05T10:18:05Z")

</div>

No problem. Let me know if it works for you.

---

<div class="post-metadata">

**Author:** ![mikey](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mikey](https://discourse.nodered.org/u/mikey)\
**Post date:** [5 February 2022 11:36 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/18 "2022-02-05T11:36:35Z")

</div>

will be a few days but ill keep you posted.

---

<div class="post-metadata">

**Author:** ![DiverRich](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diverrich/32/64523_2.png) [@DiverRich](https://discourse.nodered.org/u/DiverRich)\
**Post date:** [20 May 2022 19:12 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/19 "2022-05-20T19:12:02Z")

</div>

I've been dealing with some huemagic issues that are causing errors periodically.

> <https://github.com/Foddy/node-red-contrib-huemagic/issues/350#issuecomment-1133106417>
>
> After the update I get the following entries in the node-red log:
> 
> \`\`\`
> 22 Mar… 10:00:29 - \[info\] \[hue-bridge:Hue Wohnung\] Error requesting info from the bridge. Reconnect in some secs. undefined
> 22 Mar 10:00:29 - \[info\] \[hue-bridge:Hue Wohnung\] Initializing the bridge (192.168.178.15)…
> 22 Mar 10:00:30 - \[info\] \[hue-bridge:Hue Wohnung\] Connected to bridge
> 22 Mar 10:00:30 - \[info\] \[hue-bridge:Hue Wohnung\] Processing bridge resources…
> 22 Mar 10:00:30 - \[info\] \[hue-bridge:Hue Wohnung\] Initial emit of resource states…
> 22 Mar 10:00:31 - \[info\] \[hue-bridge:Hue Wohnung\] Keeping nodes up-to-date…
> 22 Mar 10:00:31 - \[info\] \[hue-bridge:Hue Wohnung\] Subscribing to bridge events…
> 22 Mar 10:00:51 - \[info\] \[hue-bridge:Hue Wohnung\] Error requesting info from the bridge. Reconnect in some secs. undefined
> 22 Mar 10:00:51 - \[info\] \[hue-bridge:Hue Wohnung\] Initializing the bridge (192.168.178.15)…
> 22 Mar 10:00:51 - \[info\] \[hue-bridge:Hue Wohnung\] Connected to bridge
> 22 Mar 10:00:51 - \[info\] \[hue-bridge:Hue Wohnung\] Processing bridge resources…
> 22 Mar 10:00:51 - \[info\] \[hue-bridge:Hue Wohnung\] Initial emit of resource states…
> 22 Mar 10:00:52 - \[info\] \[hue-bridge:Hue Wohnung\] Keeping nodes up-to-date…
> 22 Mar 10:00:52 - \[info\] \[hue-bridge:Hue Wohnung\] Subscribing to bridge events…
> 22 Mar 10:01:01 - \[info\] \[hue-bridge:Hue Wohnung\] Error requesting info from the bridge. Reconnect in some secs. undefined
> \`\`\`
> 
> \`\`\`
> HueMagic: Connection to bridge lost. Trying to reconnect again in 30 seconds… Event {
> type: 'error',
> status: 503,
> message: 'Service Temporarily Unavailable'
> }
> \`\`\`
> 
> HUE Bridge Version BSB002 / 1.50.1950111030
> Node Red Version 2.2.0
> 
> All scenes and/or lamps can only be switched sporadically.
> 
> Is it now a mistake or did I go about it the wrong way?
> 
> The previous version with the workaround worked stable for me. Whereby I use exclusively the switching of scenes and groups.

So I came up with a slightly different approach that restarts node-red completely. Not sure if that's a good idea or a bad idea, but it works.

 ![Screen Shot 2022-05-20 at 12.10.24 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/1263b921c5d87bfea2fbe812461ff19d39edf8b8.png)

You can attach it to a debug node, but I've never seen it output anything, so I deleted it.

What would probably work better is a script to monitor for errors and then automatically trigger the restart, but that's out of my wheelhouse.

```auto
[
    {
        "id": "c7d13f9184df6c05",
        "type": "inject",
        "z": "edce5f4c305a1484",
        "name": "Restart Node-RED Every Hour",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "3600",
        "crontab": "",
        "once": false,
        "onceDelay": "3600",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 330,
        "y": 320,
        "wires": [
            [
                "5beb96fc1c9e261b"
            ]
        ]
    },
    {
        "id": "5beb96fc1c9e261b",
        "type": "exec",
        "z": "edce5f4c305a1484",
        "command": "node-red-restart",
        "addpay": "payload",
        "append": "",
        "useSpawn": "true",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 600,
        "y": 320,
        "wires": [
            [],
            [],
            []
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![syoma755](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syoma755/32/14687_2.png) [@syoma755](https://discourse.nodered.org/u/syoma755)\
**Post date:** [24 November 2023 14:13 UTC](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341/20 "2023-11-24T14:13:22Z")

</div>

Hi all. Just wondering - this topic was solved some while ago. Meanwhile is there any build-in node availabe to do the same job?

[Next page](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341.md?page=2)
