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) {
  0{
    Write-Host "Yes"
    Get-AzResourceGroup | Remove-AzResourceGroup -Force -Verbose -AsJob
  }1{
    Write-Host "No"
  }2{
  Write-Host "Cancel"
  }
}





0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...