Skip to content

Instantly share code, notes, and snippets.

@KaiWalter
Created March 20, 2019 04:54
Show Gist options
  • Select an option

  • Save KaiWalter/fd2b73945b4eae55483284d875b28b0b to your computer and use it in GitHub Desktop.

Select an option

Save KaiWalter/fd2b73945b4eae55483284d875b28b0b to your computer and use it in GitHub Desktop.
remove ResourceGroupDeployments up to a given limit
param(
[Parameter(Mandatory = $false, Position = 1)]
[int]
$Keep = 100
)
Get-AzureRmResourceGroup | ForEach-Object {
$ts = (Get-AzureRmResourceGroupDeployment -ResourceGroupName $_.ResourceGroupName |
Sort-Object Timestamp -Descending |
Select-Object -First $Keep |
Select-Object -Last 1).Timestamp
if ($ts) {
Write-Host "delete from" $_.ResourceGroupName "older than " $ts
Get-AzureRmResourceGroupDeployment -ResourceGroupName $_.ResourceGroupName |
Where-Object {$_.Timestamp -lt $ts} |
Remove-AzureRmResourceGroupDeployment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment