Suppose you’re a an accomplished DevOps cloud architect. You build 100% of your Azure infrastructure via code. That means you’re in the enviable position of being able to blow away all traces of an environment and recreate it at will. And you probably want or need to do this regularly, for example when resetting development environments at build or release time.
This post is about making removing entire Azure environments a little faster and more convenient.
Unfortunately, there are no batch capabilities in the Azure portal that would let you easily select a bunch of resource groups and remove them (and all the contained resources).
And, unlike AWS, Azure isn’t eventually consistent when using PowerShell. That means you wait for each Remove-AzureResourceGroup
cmdlet to complete. So, it can take a very long time time to remove one-by-one in PowerShell an environment consisting of many resource groups.
The simple PowerShell script below, shown as a function, uses tags to find and remove a number of resource groups. For each resource group, it uses Start-Job
so that resource group deletion happens in parallel.
But Danger, Will Robinson! The script is indiscriminate: it blows away everything in the resource group and all resource groups containing a specific tag.
The script won’t work unless you were smart enough to tag each resource group with an “Environment” tag. That’s best practice anyway.
As always, the elegance of the PowerShell pipeline reduces the work needed to, essentially, just three lines of code.
I hope you find this useful, and Microsoft, if you are listening, how about adding AsJob
to more AzureRM PowerShell cmdlets?
<# .SYNOPSIS Deletes all Azure resource groups with a specific tag value for a tag key "Environment" .DESCRIPTION Starts PowerShell background jobs to remove an environement by tags. USE WITH CAUTION. Also, a destructive, kinda ass-backwards way to see if everything was tagged when created. When this completes, any resources remaining in the environment were NOT properly tagged. .PARAMETER -env Given a _existing_ tag in the resource group of form: $tags = @{"Environment" = "MyTestEnvironment"}, -env is $tags.Item("Environment") .EXAMPLE PS C:\> . .\FunctionRemoveEnvironment.ps1 PS C:\> Remove-Environment -env "MyTestEnvironment" .NOTES Alex Neihaus 2018-01-05 (c) 2018 Air11 Technology LLC -- licensed under the Apache OpenSource 2.0 license, https://opensource.org/licenses/Apache-2.0 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author's blog: https://yobyot.com #> function Remove-Environment ([string]$env) # Parameter is the value of a tag named "Environment" existing in all Azure resource groups which should be deleted e.g. "FinanceDev01" or "ClaimsStage03", etc. { # Finding RGs by tag assumes that an "Environment" tag was added at creation time Find-AzureRmResourceGroup -tag @{ "Environment" = $env } | ForEach-Object -Process { $rgname = $_.Name; Start-Job -ScriptBlock { Remove-AzureRmResourceGroup -Name $args[0] -Force } -ArgumentList $rgname; Start-Sleep 30 # Azure gets very upset if you slam too much work at it and randomly decides not to accept the request } Get-Job | Wait-Job | Receive-Job }
Leave a Reply