This blog post is in continuation to my previous one Resubmit your failed Power Automate flow runs automatically using M365 CLI and REST API, in this blog post let us see how to cancel all your running flow runs using
- CLI for Microsoft 365
- Power Automate REST API
CLI for Microsoft 365:
Microsoft 365 CLI helps you manage configuration settings of Microsoft 365 tenant and its various services like SharePoint, Power Automate, Power Apps, Microsoft Graph etc and to build automation scripts on any platform. Refer to this post Resubmit your failed Power Automate flow runs automatically using M365 CLI and REST API for the steps to execute & to get started with M365 CLI commands. Find below the cmdlet to cancel a flow run
CLI cmdlet to cancel a Flow Run:
Replace the flowEnvironmentID, flowGUID & flowRunID
m365 flow run cancel --environment flowEnvironmentID --flow flowGUID --name flowRunID –confirm
You can run the M365 CLI commands stored in a file like PowerShell cmdlets. Find below the M365 CLI cmdlets stored in a PowerShell file (.ps1) to cancel the running flow runs automatically.
$flowEnvironment=$args[0]
$flowGUID=$args[1]
$flowRuns = m365 flow run list --environment $flowEnvironment --flow $flowGUID --output json | ConvertFrom-Json
foreach ($run in $flowRuns)
{
if($run.status -eq "Running")
{
Write-Output "Run details: " $run
# Cancel all the running flow runs
m365 flow run cancel --environment $flowEnvironment --flow $flowGUID --name $run.name --confirm
Write-Output "Run Cancelled successfully"
}
}
The above script stored in file with .ps1 extension can be executed as shown below on the Power Shell command line by passing the Flow Environment ID and the Flow ID in the command line
PS C:\Script> ./cancelFlowRuns.ps1 flowEnvironmentId flowIdforcancellingruns
To get the Flow Environment Id and Flow Id, refer to the below screenshot
The script to cancel all ongoing flow runs can be downloaded from my GitHub here. Find below screenshot after running the script.
Power Automate REST API:
There are Power Automate REST API endpoints to list the Flow Runs and to cancel a run. Go through the following blog post for more information on how access the Power Automate REST API endpoints
Everything to know about Power Automate REST API to manage your flows
The above-mentioned blogpost will help you to call the following Power Automate REST APIs from a custom connector and programmatically from other applications.
API Endpoint to list flow runs:
Endpoint to cancel a flow run:
Summary: I would recommend getting familiar with Microsoft 365 CLI which has various cmdlets to make your job easier. The syntax of all commands is well documented with examples. Hope you have found this informational & thanks for reading. If you are visiting my blog for the first time, please do look at my other blogposts.
What does “Error: true is not a valid GUID” mean. The script is running, but I’m not convinced I have the correct identifier strings…
LikeLike
I have checked this today, it works as it should. I believe you are trying to run the M365 CLI script to cancel all runs? Have updated the post with some additional screenshots, hope it helps. Also refer to the post for the M365 login steps: https://ashiqf.com/2021/05/09/resubmit-your-failed-power-automate-flow-runs-automatically-using-m365-cli-and-rest-api/
LikeLike
Thanks for the script just and update need to append Name to –environment and –flow i.e. –environmentName and –flowName
LikeLike
Does this still work if the workflow is part of a Solution? The path to the flow now includes the Solution Guid in it. Is there a way to pass that info?
LikeLike
Very good script but yes M365 CLI might have updated their interface and defo had to amend –environment and flow to enviromentName and flowName. But overall thank you Mohammed!
LikeLike
Had to modify this slightly to run it, and the confirm flag doesn’t seem to be available.
$flowEnvironment=$args[0]
$flowGUID=$args[1]
$flowRuns = m365 flow run list -e $flowEnvironment –flowName $flowGUID –status “Running” –output json | ConvertFrom-Json
foreach ($run in $flowRuns)
{
Write-Output $run.name
Write-Output $run.properties.status
if($run.properties.status -eq “Running”)
{
Write-Output “Run details: ” $run
# Cancel all the running flow runs
“y” | m365 flow run cancel -e $flowEnvironment –flowName $flowGUID –name $run.name
Write-Output “Run Cancelled successfully”
}
}
LikeLike
The powershell script doesn’t work i’m afraid for me.
I wrote a blogpost regarding authentication it has changed.
https://clouddevelopmentthomasvnoppen.blogspot.com/
LikeLike
Hello!
I tested REST API, it works.
https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/FlowEnvironment/flows/FlowGUID/runs?api-version=2016-11-01
One question, how to filter the asnwer? I’d like to get only Running Flows (Not Cancelled..)
LikeLike
https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/FlowEnvironment/flows/FlowGUID/runs?api-version=2016-11-01&$filter=Status%20eq%20%27running%27
LikeLike