Skip to content

Instantly share code, notes, and snippets.

@mawiseman
Last active May 2, 2019 07:13
Show Gist options
  • Save mawiseman/65c8dcc4c9d23bdf2b9d527355094cfa to your computer and use it in GitHub Desktop.
Save mawiseman/65c8dcc4c9d23bdf2b9d527355094cfa to your computer and use it in GitHub Desktop.
Delete Azure Deployments
# README
# This script will delete all deployments in an Azure resource group older than X Days
# You can use this script to count hom many deployments exist: Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup | measure
# Variables
$subscriptionName = '{{azure subscription name}}'
$resourceGroupName = '{{azure resource group name}}'
$daysOldToDelete = -150
$concurrentJobs = 4
# START
# Login and Save Credentails
Write-Host "Authenticating against Azure"
$path = ".\azureprofile.json"
Login-AzureRmAccount
Save-AzureRmProfile -Path $path -Force
# Select the Subscription
Write-Host "Selecting Azure Subscription: $subscriptionName"
Select-AzureRmSubscription $subscriptionName | Out-Null
# Get all the deployments then filter
Write-Host "Getting Deployments for '$resourceGroupName' older than '$daysOldToDelete' days"
$deployments = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName
$deploymentsToDelete = $deployments | Where-Object { $_.Timestamp -lt ((get-date).AddDays($daysOldToDelete)) }
Write-Host "Deployments found: $($deployments.Length)"
Write-Host "Deployments to delete: $($deploymentsToDelete.Length)"
# Delete filterd deployments in parallel
$counter = 0
$start = Get-Date
[nullable[double]]$secondsRemaining = $null
foreach ($deploymentToDelete in $deploymentsToDelete) {
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -ge $concurrentJobs) {
$running | Wait-Job -Any | Out-Null
# estimate the time remaining (we can only start do this when the jobs start to complete)
$secondsElapsed = (Get-Date) - $start
$secondsRemaining = ($secondsElapsed.TotalSeconds / $counter) * ($deploymentsToDelete.Count - $counter)
}
$counter++
$percentComplete = (($counter / $deploymentsToDelete.count) * 100)
$progressParameters = @{
Activity = "Deleting Deploments"
Status = "Deleting $($deploymentToDelete.DeploymentName)"
PercentComplete = $percentComplete
}
if ($secondsRemaining) {
$progressParameters.SecondsRemaining = $secondsRemaining
}
Write-Progress @progressParameters
Write-Host $progressParameters.CurrentOperation
Start-Job {
param($resourceGroupName, $deploymentName, $path)
Select-AzureRmProfile -Path $path
Remove-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -DeploymentName $deploymentName
} -ArgumentList @($resourceGroupName, $deploymentToDelete.DeploymentName, $path) | Out-Null
}
# Wait for all jobs to complete and results ready to be received
Wait-Job * | Out-Null
# Process the results
foreach($job in Get-Job)
{
$result = Receive-Job $job
Write-Host $result
}
Remove-Job -State Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment