Remove all your resource groups and hence their contents in Azure

Written by James McDonald

September 25, 2019

You can do this two ways either with one line of power shell code or more safely by wrapping it in a script that first checks which subscription you are logged into and then ask you nicely if you really want to delete all the resource groups in your Subscription.

How to unsafely clean up after you have done a heap of testing in your Azure account

Get-AzResourceGroup | Remove-AzResourceGroup -Force -Verbose -AsJob

How to less unsafely clean up after you have done a heap of testing in your Azure account

## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Deletes all resource groups."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Exit without doing anything."
# $cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$subscription = Get-AzSubscription
## Use the following each time your want to prompt the use
$title = "Warning: All Azure Resource Groups will be removed from:
`t Account: `"$($subscription.ExtendedProperties.Account)`"
`t Subscription: `"$($subscription.Name)`""
$message = "Are you sure you want to do this?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    Write-Host "Yes"
    Get-AzResourceGroup | Remove-AzResourceGroup -Force -Verbose -AsJob
    Write-Host "No"
  Write-Host "Cancel"


Submit a Comment

Your email address will not be published.

You May Also Like…

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...

PHP array_map Multiple Arrays

array_map can take multiple arrays. I like how it starts mapping through them starting at the first element of each...