How to delete ALL Azure resources with powershell

As resources in Azure are grouped into resource groups(RG), that would probably be the easiest way to go about this. Use these cmdlets to do this.

  • Get-AzureRmResourceGroup

  • Remove-AzureRmResourceGroup

Once you have retrieved all the RGs, you can pipe the results with the | character to the Remove cmdlet and iterate through them with a ForEach loop. Give it a go, it is the best way to learn, as opposed to simply asking for the solution on here.

Alternatively, if you don't want to use powershell, just delete your RGs from the portal. I assume you think it would take too long because you are looking at the individual resources and not their RGs, but if you really do have that many RGs, then scripting is best.


#It will delete all resources without asking any confirmation
Login-AzureRmAccount

$rgName = Get-AzureRmResourceGroup 

Foreach($name in $rgName)
{
Write-Host $name.ResourceGroupName
Remove-AzureRmResourceGroup -Name $name.ResourceGroupName -Verbose -Force
}

A script like that could be really harmful... but also very useful.

I've created a little script and add little security on it to avoid nuking the wrong subscription.

The script asks you to login-in then list all the subscriptions that this account has access. Once you specify which one, it will list all the resource grouped by resource group. Then as a final warning, it will require one last validation before nuking everything.

# Login
Login-AzureRmAccount 

# Get a list of all Azure subscript that the user can access
$allSubs = Get-AzureRmSubscription 

$allSubs | Sort-Object SubscriptionName | Format-Table -Property SubscriptionName, SubscriptionId, State


$theSub = Read-Host "Enter the subscriptionId you want to clean"

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

#Get all the resources groups

$allRG = Get-AzureRmResourceGroup


foreach ( $g in $allRG){

    Write-Host $g.ResourceGroupName -ForegroundColor Yellow 
    Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow 
    $allResources = Find-AzureRmResource -ResourceGroupNameContains $g.ResourceGroupName

    if($allResources){
        $allResources | Format-Table -Property Name, ResourceName
    }
    else{
         Write-Host "-- empty--`n"
    } 
    Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow 
}


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

if($lastValidation.ToLower().Equals("yes")){

    foreach ( $g in $allRG){

        Write-Host "Deleting " $g.ResourceGroupName 
        Remove-AzureRmResourceGroup -Name $g.ResourceGroupName -Force -WhatIf

    }
}
else{
     Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}

The code is available on GitHub: AzurePowerTools