Created
February 6, 2019 18:42
-
-
Save bmoore-msft/ed33fb940dafb09380174b7fca57651f to your computer and use it in GitHub Desktop.
Delete Deployments from a ResourceGroup Based on a Max Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
#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++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good catch - see this one for an approach that uses jobs.