Skip to content

Instantly share code, notes, and snippets.

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
[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.