Cancel all your running Power Automate flow runs using M365 CLI and REST API

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:

GET https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{FlowEnvironment}/flows/{FlowGUID}/runs?api-version=2016-11-01

Endpoint to cancel a flow run:

POST https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{FlowEnvironment}/flows/{FlowGUID}/triggers/manual/histories/{FlowRunID}/cancel?api-version=2016-11-01

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.

9 thoughts on “Cancel all your running Power Automate flow runs using M365 CLI and REST API

  1. 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…

    Like

  2. Thanks for the script just and update need to append Name to –environment and –flow i.e. –environmentName and –flowName

    Like

  3. 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?

    Like

  4. 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!

    Like

  5. 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”
    }
    }

    Like

Leave a comment