Cancelling all running flows in Power Automate

Finding out that you have a scheduled flow in Power automate or Microsoft Flow (What ever name it has this week), that just continue to run and then the next flow dows the same and all of a sudden, you have a gazillion flows running and never stopping. Well according to the Power automate limits, they will be cancelled after 30 days.

But you don’t have the option to turn the flow off and the wait 30-days until you can turn it back on again – I assume. And you have a list that look like this:

The behavior mostly happens, when a workflow starts, before the previous has finished. You can take a look at this article by Pieter Veenstra on how to troubbleshoot the flow to dig deeper into the how and the why. Here is a few ways to prevent it and to deal with it.

  1. Set the degree of parallelism to 1, then you will only have one flow running. Then you will discover immediately if it hangs.
  2. Cancel all the running flows
  3. Make a copy of your flow, export it, then import it and delete your old flow (not a fan of this option but it will work)
  4. Check if lists or items or sites etc. Exists before you try to create them. If you need to check for new items in a list and run stuff for each item, then make a condition to only run, if there is any items.
  5. Make a list, with a single item that reads “locked” or “Unlocked”, then at step 1, check if item is unlocked, if not, terminate the flow. If “unlocked = true, then set it to locked, and then run flow. As a final step, set the item back to “unlocked”.

Set the degree of parallelism to 1

  1. If you have a recurring flow, click on the steps menu “…” and the “Settings”.

2. Activate “Concurrency Control” and then set the “Degree of Parallelism” to 1

Cancel all the running flows

Thank you to the ever brilliant users in the “Microsoft Power Automate Community” for delivering this solution. This is just brilliant.

  1. Navigate to the flow in question
  2. Click on “All runs”
  3. Filter the view to only show the running flows
  4. Open the console in the browser (hit F12 and the select console)
  5. Paste in the code below. For some reason I need to paste the jquery part twice before the actual action code, but it works like a charm. Depending on the number of flows you have running, this can take quite a while.
//Include jquery (you may need to paste this following twice)
var jq = document.createElement(‘script’);
jq.src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”;
document.getElementsByTagName(‘head’)[0].appendChild(jq);
// Cancel all running flows ( This part you only paste once)
confirm = function () {return true;};
setInterval(function () {
$(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).first().click();
$(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).last().click();
$(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).first().click();
$(‘button[name=”Cancel flow run(s)”]’).click();
},3000);

32 thoughts on “Cancelling all running flows in Power Automate”

  1. We need to know who the active site owners are of more than 4000 sites. I had a flow that sent an approval to all site owners of sites (we ran 100 at a time) and some sites have multiple owners so it looped through. After 3 days of no response it escalated to the owners manager with another approval. The approval was Yes, I own the site or No, I don’t own this site. A lot of people didn’t reply so may escalated to managers. I have some managers who own 100-400 sites so there were getting bombarded with emails. (I am going to rewrite the flow!). I ran this code and it stopped more than 700 flows from running and saved me from dealing with irate managers! Thank you!

    Reply
    • Hi Bob. I am so glad that this little trick could help you out. Thank you for taking the time to write a comment and letting me know that it made a difference.

      Reply
  2. Stumbled upon this when trying to cancel A LOT of running flows that was triggered due to a bulk update.
    Just some questions; will the canceling of runs be updating “live” or only when it’s completed? Also will it try to cancel the oldest or newest run first?

    Reply
  3. Great !! Thanks a lot. Tip: change the label according to the language (ex: ITA: ‘Running’ –> ‘In esecuzione’, ‘Cancel’ –> ‘Annulla’)

    Reply
  4. I’m trying to use this, in Edge. I did have to push in the jquery twice, but after that, when I’m executing the second script, the screen doesn’t show anything. on the console, though, i’m getting errors:

    main.279ec6d3.js:2

    Uncaught TypeError: Cannot convert undefined or null to object
    at Function.getOwnPropertyNames (<anonymous>)
    at K (main.279ec6d3.js:2:130985)
    at W (main.279ec6d3.js:2:130613)
    at i (main.279ec6d3.js:2:173915)
    at main.279ec6d3.js:2:174015

    does anyone have any insight into this?

    tks

    Reply
  5. I tried running this in Chrome and it seems that there is an alert dialog that gets in the way of this working. Perhaps, this was added after the post was written.
    I was curious about why there was a function in the script “named” confirm that just returns true. I’m not clear on the purpose of that part, it doesn’t seem to be used anywhere.

    Reply
  6. Hi, thanks for this.

    I have 120000 flows to cancel. This code is running ok but it is not deleting any of the running runs unless I keep manually changing selections. Am I missing something?

    thanks,
    Sean

    Reply
    • You should be able to go into “All runs – Running” and then only get the running flows. From there you can run the code, but I don’t know how many it will process before the list is empty and you need to repeat the action. 120000 flows is going to take some time cleaning up though.

      Alternatively you could try this:
      1. export the flow
      2. import the flow and then set it up again
      3. delete the original flow. That “should” delete all the running instances.

      Disclaimer: I haven’t tried this myself so results may vary.

      Reply
  7. After I select the first item – it appears to be working (screen flashing)…but it’s not actually deleting any? Did something change? I never see the confirmation box like when I do them manually.

    Reply
  8. The button named changed so be sure to updated the code so that it looks for the new name “$(‘button[name=”Cancel flow run(s)”]’).click();”. Also I removed the “jQuery.noConflict()” part since that was causing issues. After that, click the first item in the list so that the Cancel button shows up and then it should start working.

    Reply
  9. Hey everyone, looks like the html has changed on the page, which breaks the targeting that the jquery does. Basically the name of the cancel button changed, and the amount of nesting for each row changed. Here’s the updated code that worked for me:

    //step 1. only need to past this once in chrome
    var jq = document.createElement(‘script’);
    jq.src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”;
    document.getElementsByTagName(‘head’)[0].appendChild(jq);

    //step 2.
    confirm = function () {return true;};
    setInterval(function () {
    $(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).first().click();
    $(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).last().click();
    $(“.fl-StatusInCell:contains(‘Running’)”).parent().parent().parent().find(‘.ms-DetailsRow-cell’).first().click();
    $(‘button[name=”Cancel flow run(s)”]’).click();
    },3000);

    Reply
    • Hi Matt

      Thank you for the edit. I’ve updated the code in the post. I’ve deleted the other comments you made so it doesn’t post twice 🙂

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.