About Me

My photo
I am an MCSE in Data Management and Analytics, specializing in MS SQL Server, and an MCP in Azure. With over 19+ years of experience in the IT industry, I bring expertise in data management, Azure Cloud, Data Center Migration, Infrastructure Architecture planning, as well as Virtualization and automation. I have a deep passion for driving innovation through infrastructure automation, particularly using Terraform for efficient provisioning. If you're looking for guidance on automating your infrastructure or have questions about Azure, SQL Server, or cloud migration, feel free to reach out. I often write to capture my own experiences and insights for future reference, but I hope that sharing these experiences through my blog will help others on their journey as well. Thank you for reading!

How to delete all resources from all resource group in a specific subscription[dangerous interactive script that delete all resources]


How to delete all resources from all resource group in a specific subscription[dangerous interactive script that delete all resources]





#================================================================
#= Very dangerous interactive script that delete all rescources 
#= from all rescourcegroup in a specific subscription
#================================================================

# How to install and configure Azure PowerShell
# https://docs.microsoft.com/en-us/powershell/azureps-cmdlets-docs/

# Login
Login-AzureRmAccount 

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

$allSubs | Sort-Object Name | Format-Table -Property Name, 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 
    
    # Using -ODataQuery is compatible with AzureRM 5.x and AzureRM 6.x. -ResourceGroupName is only in 6.x
    $query = "`$filter=resourceGroup eq '{0}'" -f $g.ResourceGroupName
    $allResources = Get-AzureRmResource -ODataQuery $query
    
    if($allResources){
        $allResources | Format-Table -Property Name, ResourceName
    }else{
         Write-Host "-- empty--`n"
    } 
    Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow 
}

#exit

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

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

    foreach ( $g in $allRG){

        Write-Host "Deleting " $g.ResourceGroupName 
        # Last safety, you need to remove the -WhatIf parameter to really delete the resources
        Remove-AzureRmResourceGroup -Name $g.ResourceGroupName -Force -WhatIf

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

No comments: