Remove Subscription Content From Azure


My friend Frank created this nifty little PowerShell script to delete all of the Resource Groups in an Azure subscription by selecting the right subscription name. Here is the original post:

Frank’s script is fantastic and straightforward when you use your Azure subscription for demos or presentations and need to create and destroy frequently. I needed it to do a few things extra: I modified the script to provide the subscription name on execution and added a check to see if the resource group is locked. Resource locks are used to safeguard critical workloads that I am using and don’t wish to delete.


# Login
    Write-Host "Checking for active session" -ForegroundColor Cyan
    Get-AzureRmContext | Out-Null
catch [System.Management.Automation.PSInvalidOperationException]
    Write-Warning -Message 'No session detected. Prompting for login.'
    throw $_

Write-Host "You select the following subscription. (it will be display 15 sec.)" -ForegroundColor Cyan
Get-AzureRmSubscription -SubscriptionName $SubscriptionName | Select-AzureRmSubscription 

#Get all the resources groups, with the exception of the cloud shell
$allRG = Get-AzureRmResourceGroup | Where-Object ResourceGroupName -NotLike "*cloud-shell*"

#Gather all Resource Groups and check to see if there is a lock
$Results = foreach ($RG in $allRG) {
    $ResourceLocks = Get-AzureRmResourceLock -ResourceGroupName $RG.ResourceGroupName

    If ($ResourceLocks.count -eq 0) {
        $RGResourceLock = 'No'
    else {
        $RGResourceLock = 'Yes'

        ResourceGroup = $RG.ResourceGroupName
        ResourceLock = $RGResourceLock

$Results | FT -autosize

$lastValidation = Read-Host "Do you wich to delete ALL the resouces previously listed that are not locked? (YES/ NO)"

if($lastValidation.ToLower().contains("y")) {
    foreach ( $rg in $Results){

        If ($RG.ResourceLock -ne "Yes") {
            Write-Host "Deleting " $rg -ForegroundColor Cyan 
            Remove-AzureRmResourceGroup -Name $rg.ResourceGroup -Force -WhatIf
     Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan

The code is also available on Github:

Here is an example of me running the script in the Azure Cloud Shell. Script