DEV Community

Cover image for Cancel All Running Flows with One Click in Power App
Mark Nanneman
Mark Nanneman

Posted on • Edited on

Cancel All Running Flows with One Click in Power App

Cancel All Running Flows with One Click in Power App

January 25, 2025

The Problem

Have you ever had a runaway flow? I know I have.

Image description

Back in the early days, I would occasionally make a mistake resulting in an infinite loop, or a child flow being triggered a ridiculous amount of times.*

*Okay, TBH I occasionally still do this when I’m horsing around in my development environment

Sometimes the actions in the runaway flow start hitting throttle limits and they slow down, resulting in many flows stuck in a running status. This can impact the performance of your entire ecosystem and be very bad.

Canceling all the runs used to be a tedious processing of selecting them in batches of 20, or saving a copy of the runaway flow and then deleting it.

The Solution

Fortunately, Microsoft has added a “Cancel all flow runs” button.

Image description

This is great. Even better, there is an API endpoint you can use to cancel all flow runs for a selected flow. You can even find the link when you list your “workflows” (Processes) table in Dataverse:

Image description

Here is the endpoint:

https://<your_domain>.crm.dynamics.com/api/data/v9.1/workflows(<flow_name>)/Microsoft.Dynamics.CRM.CancelAllCloudFlowRuns

All you need to do is send an authorized POST to the url, and you will trigger the immediate cancellation of all running flows for the flow named in the url. You can do this in Power Automate or Power Apps with an Invoke an HTTP Request with Entra ID (preauthorized).

Example – Canvas App

Here’s how it works in a Power Apps Canvas App.

I have a Canvas App I’m building to manage Power Automate Cloud Flows in any environment. It uses the Dataverse “workflows” (Processes) table to list all flows in the environment, as well as HTTP calls to list all flow runs. In the screenshot below, I have opened up a flow called “Delay Flow Test” to view all the recent runs that belong to it.

Image description

The HTTP with Microsoft Entra ID (preauthorized) Invoke HTTP Action

I have added a “Cancel all flow runs” icon:

Image description

In its simplest version, the expression for “OnSelect” is as simple as this:

Image description

'HTTPwithMicrosoftEntraID(preauthorized)'.InvokeHttp("POST",$"/api/data/v9.1/workflows({loc_sel_flow.Process})/Microsoft.Dynamics.CRM.CancelAllCloudFlowRuns")

I’ve updated it to only run if there are running flows, and to send a notification. I’ve also moved the code to a confirmation popup to make sure the user wants to cancel all running flows.

In the end, this is what my code looked like:

Image description

This gets a count of the running flows that will be canceled, and performs a refresh of the select flow’s run history so that we can get immediate confirmation that the API action worked.

Make Sure Your “HTTP with Microsoft EntraID” connector is set up with a connection to your Dynamics Url, i.e. (https://<your_domain>.crm.dynamics.com)

Click Confirm to Invoke the HTTP and refresh flow runs

I click Confirm and receive this notification:

Image description

Flow history is immediately refreshed:

Image description

The flows that were submitted to the bulk cancellation API now show a “Cancelling” status.

Image description

Hope this helps!

If you found this post helpful please give it a like.

Power Platform Developer | WordPress | LinkedIn | YouTube: Mark’s Power Stuff | Buy me a coffee

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay