问题
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.
回答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.
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.
回答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
}
回答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
回答4:
switch to the poweshell shell in Azure and run this command to wipe everything..
Get-AzureRmResourceGroup | Remove-AzureRmResourceGroup -verbose -Force
回答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
回答6:
Updated for new Azure PowerShell module Az
# Login
Connect-AzAccount
# Get a list of all Azure subscript that the user can access
$allSubs = Get-azSubscription
$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-azSubscription -SubscriptionId $theSub | Select-azSubscription
#Get all the resources groups
$allRG = Get-azResourceGroup
foreach ( $g in $allRG){
Write-Host $g.ResourceGroupName -ForegroundColor Yellow
Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow
$allResources = Get-azResource -ResourceGroupName $g.ResourceGroupName | FT
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
Get-AzResourceGroup -Name $g.ResourceGroupName | Remove-AzResourceGroup -Verbose -Force
}
}
else{
Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}
回答7:
To remove all resources from Azure Resource Group but to keep the group with its settings:
Get-AzResource -ResourceGroupName $ResourceGroupName | Remove-AzResource -Force
回答8:
Below command can be used to delete all resources
Get-AzResource | Remove-AzResource -force
回答9:
here is a one-line (please log in using az login)
az group list | ConvertFrom-Json | % {az group delete --name $_.name -y}
来源:https://stackoverflow.com/questions/36403597/how-to-delete-all-azure-resources-with-powershell