Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete Deployments from a ResourceGroup Based on a Max Number
#
#this script will delete deployments from a resourceGroup if the number of deployments exceeds the number specified by the Max parameter
#
Param(
[string] [Parameter(Mandatory=$true)] $ResourceGroupName,
[int] [Parameter(Mandatory=$true)] $Max
)
$deployments = Get-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName
if($deployments.Count -gt $Max){
$numToDelete = $deployments.Count - $Max
$i = 1
while($i -le $numToDelete){
write-host "Deleting Deployment: "$deployments[$deployments.Count - $i].DeploymentName
Remove-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -Name $deployments[$deployments.Count - $i].DeploymentName -Verbose
$i++
}
}
@octobeau
Copy link

octobeau commented Sep 16, 2019

For whatever reason, deleting Deployments takes a significant amount of time. I'll be looking at refactoring this to take advantage of parallel threaded deletes.

@bmoore-msft
Copy link
Author

bmoore-msft commented Sep 16, 2019

Good catch - see this one for an approach that uses jobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment