Virtualization, Cloud, Infrastructure and all that stuff in-between

My ramblings on the stuff that holds it all together

POSH1Liner – Remove all Resource Groups and contents

Ever had a need to clean up and remove a bunch of resource groups and all the resources they contain? – often a problem after a period of frequent, err experimenting/failing.

CAUTION – use at your own risk, this could easily wipe out all your resources – even the stuff you want

1st – check you’re targeting the correct subscription – use

Set-AzContext -SubscriptionId <YOUR_SUBS_ID>

Then – MAKE SURE You have the correct -Name pattern. If you omit the -Name parameter you’ll delete EVERYTHING in the target subscription (although it will prompt for a yes/no on each one to be safe (you can add the -Force parameter to not do this if you’re feeling brave)

In this example all my RGs start with myResources (myResources1, myResources2 etc.) and we use a wildcard ‘*’ to do this pattern matching.

Get-AzResourceGroup -name myResources* | Remove-AzResourceGroup

If you want to do a dry run to be absolutely sure which resource groups it will delete you can also add the -WhatIf parameter to the Remove-AzResourceGroup command

Leave a comment