Thomas VineThomas Vine Power Platform Developer

M365 CLI: Cancel all running flow runs

Have you ever found yourself stuck in an endless cycle of Power Automate flows that just won’t stop running? Don’t worry; there’s a solution! In this blog post, we’ll introduce you to the M365 CLI, an excellent tool for managing and interacting with the Microsoft ecosystem. We’ll walk through the process of cancelling all running flow runs, bringing you one step closer to resolving issues which are often caused by excessive actions and ‘Apply to each’ loops.

List of power automate flows runs that have the status running
Flow runs currently running that we want to cancel

First, make sure to turn off the flow to prevent triggering any future flow runs. If you prefer a quick and straightforward option, you can modify the flow and re-import the solution to reset the flow. However, let’s explore a more sophisticated approach using the M365 CLI.

Install & Authenticate M365 CLI

The CLI for Microsoft 365 is an open-source project not affiliated with Microsoft, but serves as an excellent tool for interacting with the M365 ecosystem.

Before we dive in, make sure you have Node.JS installed on your system. You can download the installer from the official website. If you want to check if you already have Node.JS installed, type npm into PowerShell and check if that command is recognised.

Next, open PowerShell and enter the following command to install the M365 CLI dependencies:

npm install -g @pnp/cli-microsoft365

Once installed, you need to authenticate the M365 CLI. Simply run the following code, and it will prompt you to visit https://microsoft.com/devicelogin and enter a unique code:

m365 login

Cancelling all flow runs

After successful authentication, lets proceed to cancel all running flow runs. To do this, you need to be a Power Platform Environment Admin. First, let’s extract the environment ID and flow ID from the flow’s URL. In the example below, the GUID after environments/ is the environment ID, and the GUID after cloudflows/ is the flow ID.

https://make.powerapps.com/environments/5ec55a93-ec21-6d97-137e-1539a57e91e5/solutions/2939b1e1-112e-24cb-b9ce-25b40ef22c6d/objects/cloudflows/8e2bd99f-ec2f-8891-63b1-694f1b923d38/view

To cancel all running flow runs, we’ll combine two commands. The first command retrieves a list of all flow runs currently running. The second command cancels each of these runs:

$flowEnvironment = "EnvironmentGUID"
$flowGUID = "FlowGUID"
$flowRuns = m365 flow run list --environmentName $flowEnvironment --flowName $flowGUID --status Running --output json | ConvertFrom-Json
foreach ($run in $flowRuns) {
  Write-Output "Run details: " $run
  # Cancel all the running flow runs
  m365 flow run cancel --environmentName $flowEnvironment --flowName $flowGUID --name $run.name --confirm
  Write-Output "Run Cancelled successfully"
}

For our scenario, we just need to update the first 2 variables so it looks like the following;

$flowEnvironment = "5ec55a93-ec21-6d97-137e-1539a57e91e5"
$flowGUID = "8e2bd99f-ec2f-8891-63b1-694f1b923d38"
$flowRuns = m365 flow run list --environmentName $flowEnvironment --flowName $flowGUID --status Running --output json | ConvertFrom-Json
foreach ($run in $flowRuns) {
  Write-Output "Run details: " $run
  # Cancel all the running flow runs
  m365 flow run cancel --environmentName $flowEnvironment --flowName $flowGUID --name $run.name --confirm
  Write-Output "Run Cancelled successfully"
}

Upon running this command, the console will confirm the cancellation of each flow run. We can verify this by checking the flow run history in Power Automate.

Flow runs cancelled by M365 CLI
List of flow runs cancelled by the CLI for M365

Conclusion

By utilizing the powerful M365 CLI, you can take control of your Power Automate flow runs. Whilst you have everything set-up to run the M365 CLI, check out the online documentation for other commands and sample scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close