How to delete ALL Azure resources with powershell
Solution 1
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.
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.
Solution 2
#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
}
Solution 3
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
Solution 4
switch to the poweshell shell in Azure and run this command to wipe everything..
Get-AzureRmResourceGroup | Remove-AzureRmResourceGroup -verbose -Force
Solution 5
I know the ask was for Powershell, but if anyone is interested here is for Azure CLI
#!/bin/bash
# NOTE: Be careful as this code in intended to delete ALL Resources in a subscription. Use at your own risk.
# Set The correct Subscription
az account set -s "<Subscription_name / Id>"
# Get All resource groups and loop to delete them
for rg_name in `az group list -o tsv --query [*].name`; do
echo Deleting ${rg_name}
az group delete -n ${rg_name} --yes --no-wait
done
Related videos on Youtube
Indigo8
Updated on September 16, 2022Comments
-
Indigo8 over 1 year
I need to empty my Azure account from all resources and there's too much to remove individually in the portal. Looking for a powershell script to do this. Thanks.
-
Pianistprogrammer about 6 yearsIs there a way to delete selected Resource Groups like; get an array of RG names and delete once. I don't want to delete all the RGs. Please if there is a way to do this, I'll appreciate if you can show it here
-
Edward Rixon about 6 yearsI guess you want something like: $rgNames = "name1","name2" foreach($name in $rgNames){ Get-AzureRmResourceGroup | Where ResourceGroupName -like $name }. This gets all RGs that have names that start with that variable value. Then you can do the same as the answer, pass the result to the remove cmdlet to delete it.