Deleting an Unused Flow using PowerShell

Loading

Power Automate flows can accumulate over time, leading to unused or obsolete workflows that consume resources. Using PowerShell, you can identify and delete these flows efficiently. This guide will walk you through the step-by-step process of deleting an unused flow in Power Platform using PowerShell.


Step 1: Install Required PowerShell Modules

Before deleting a flow, ensure you have the necessary PowerShell modules installed:

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -AllowClobber
Install-Module -Name Microsoft.PowerApps.PowerShell -Force -AllowClobber

If prompted, press Y to confirm the installation.


Step 2: Authenticate to Power Platform

To interact with Power Automate, sign in with admin credentials:

Add-PowerAppsAccount

Alternatively, if using Microsoft Graph, connect with:

Connect-MgGraph -Scopes "User.Read.All", "Application.Read.All"

Step 3: Identify the Target Environment

Before deleting a flow, identify which environment it belongs to. List all environments using:

Get-AdminEnvironment | Select-Object DisplayName, EnvironmentName | Format-Table -AutoSize

🔹 Note the Environment Name where the flow is located.


Step 4: List All Flows in an Environment

To find the unused flows, list all flows in a specific environment:

$EnvironmentName = "<YourEnvironmentID>"  # Replace with your environment ID

Get-AdminFlow -EnvironmentName $EnvironmentName | Select-Object DisplayName, FlowName, CreatedTime, LastModifiedTime, State | Format-Table -AutoSize

🔹 Look for flows with old LastModifiedTime or State as Disabled, indicating they are likely unused.


Step 5: Delete an Unused Flow

Once you have identified the FlowName, delete it using:

$FlowName = "<FlowID>"  # Replace with the actual Flow ID
Remove-AdminFlow -EnvironmentName $EnvironmentName -FlowName $FlowName -Confirm:$false

Write-Host "Flow '$FlowName' has been successfully deleted."

This command deletes the specified flow permanently.


Step 6: Delete Multiple Unused Flows in Bulk

If you need to delete multiple unused flows, filter and delete them in bulk:

$EnvironmentName = "<YourEnvironmentID>"
$UnusedFlows = Get-AdminFlow -EnvironmentName $EnvironmentName | Where-Object { $_.State -eq "Disabled" -and $_.LastModifiedTime -lt (Get-Date).AddMonths(-6) }

foreach ($Flow in $UnusedFlows) {
Remove-AdminFlow -EnvironmentName $EnvironmentName -FlowName $Flow.FlowName -Confirm:$false
Write-Host "Deleted Flow: $($Flow.DisplayName)"
}

Write-Host "All unused flows deleted successfully!"

This script:

Identifies flows disabled for over 6 months
Deletes them in bulk


Step 7: Verify Flow Deletion

To ensure the flow is deleted, run:

Get-AdminFlow -EnvironmentName $EnvironmentName | Where-Object { $_.FlowName -eq $FlowName }

🔹 If the flow doesn’t appear, it has been successfully deleted.


Step 8: Automate Flow Cleanup on a Schedule

To regularly delete unused flows, schedule an automated cleanup task:

$ScriptPath = "C:\PowerAutomate\DeleteUnusedFlows.ps1"

$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 2am
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File $ScriptPath"
Register-ScheduledTask -TaskName "WeeklyFlowCleanup" -Trigger $Trigger -Action $Action -RunLevel Highest -User "NT AUTHORITY\SYSTEM"

🔹 This will automatically delete unused flows every Sunday at 2 AM.


Step 9: Disconnect PowerShell Session

After completing the deletion process, disconnect your session:

Disconnect-MgGraph
Disconnect-PowerAppsAccount

Leave a Reply

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