Skip to content

Instantly share code, notes, and snippets.

@bmoore-msft
Created February 6, 2019 18:42
Show Gist options
  • Save bmoore-msft/ed33fb940dafb09380174b7fca57651f to your computer and use it in GitHub Desktop.
Save bmoore-msft/ed33fb940dafb09380174b7fca57651f to your computer and use it in GitHub Desktop.
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

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

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