Hello everyone and welcome to my blog. In today’s blog I will discuss an interesting way to cancel all running instances of a Power Automate flow in Bulk using Power Shell.
So let’s get started. Here is my Power Automate flow which currently have few running instances. My intention is to cancel all the running instances.
While it is possible to do it manually, the problem arises when there is a significant number of flow runs to be cancelled. In scenarios like this, automation helps. So let’s see how we can automate this.
To do this, I will use the Microsoft 365 CLI. Open Power Shell or Power Shell (ISE) as administrator. The first step is to install the Microsoft 365 CLI from npm. To do that execute the below command.
npm i -g @pnp/cli-microsoft365
Once the CLI is installed successfully, the next step is to login. To login use the below command.
m365 login
When you run this command, you will be asked to login to https://microsoft.com/devicelogin and enter the code provided. Use the browser to login using the code and credentials.
You will be shown a consent page to allow the app permissions on the environment. Click Accept to continue.
If you get an error when executing the command m365 login, use the following command to set the ExecutionPolicy to unrestricted
Set-ExecutionPolicy unrestricted
After that when you run the m365 login command it should work fine.
Once your login is completed, the next step is get all the running the instances of the flow. To do this execute the below command.
$currentFlowRuns = m365 flow run list --environmentName <Guid of the environment> --flowName <GUID of the flow> --output json | ConvertFrom-Json
Once this command executes successfully, the next step is to go through each of the flows and check the status of the flow run. If the flow run status is Running, then cancel the flow. To do that, use the below lines of code.
foreach ($run in $currentFlowRuns) {
if ($run.status -eq "Running") {
# Cancel all the running flow runs
m365 flow run cancel --environmentName <guid of environment> --flowName <guid of environment> --name $run.name --confirm
Write-Output "Flow Run has been cancelled successfully"
}
}
Once the above script executes, you will find all the flow runs are cancelled successfully.
Hope you liked this post. If this post has helped, you can buy me a coffee.
For similar topics on Microsoft.NET and Power Platform, subscribe to my blog using the Subscribe option on right pane.
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Pingback: How to cancel all pending approvals in Power Automate flow - Debajit's Power Apps & Dynamics 365 Blog