Cleaning up an Azure Deployment

You most likely know this scenarios: you have deployed a nice environment into Windows Azure. Just a few AD/DC, a few front-end servers, a couple of application servers and of course two or three SQL Servers.

Now you don’t need the VMs any more and you want to remove everything. And you spend the rest of the afternoon clicking around in the Management Portal, first to remove all the VMs. They wait around until the Disks are no long registered as being attached to the VM and then delete the Disks and the underlying VHD-files.

Would it not be nice, if this could be done in a single command (or maybe two if you just wanted to remove the VMs)?

Look no further. I have created two small PowerShell scripts that will do exactly this: Remove VMs and remove Disks and underlying VHDs.

Let us first remove the VMs.

$remove = $false

if(-not $remove)
{
    Write-Host "Are you sure you want to do this?"
    Write-Host "Change bool to true"
    return
}

$serviceName = <Service Name>

$azureVMs = Get-AzureVM –Serviceame $serviceName | select Name

foreach($azureVM in $azureVMs)
{
    Remove-AzureVM -ServiceName $serviceName -Name $azureVM
}

I like to have a safe guard at the top of my scripts. Set the constant to the name of your Cloud Service (the $serviceName variable).

To remove all Disks and underlying VHD-files for VMs having belonged to a given Cloud Service run the following:

$remove = $false

if(-not $remove)
{
    Write-Host "Are you sure you want to do this?"
    Write-Host "Change bool to true"
    return
}

$serviceName = <Service Name>
$azureDisks = Get-AzureDisk | select DiskName, AttachedTo

foreach($azureDisk in $azureDisks)
{
    if($azureDisk.AttachedTo.HostedServiceName -eq $serviceName)
    {
        Remove-AzureDisk -DiskName $azureDisk -DeleteVHD
    }
}

If you have played around with the scripts for the automated Share Point deployment found at GitHub I have created a few scripts that from the created configuration files will Export all settings, remove the VMs and (re)deploy them. More about this in a later post.

About strobaek

.NET developer/architect. Runner, espresso drinker and lover of gourmet food.
This entry was posted in Azure. Bookmark the permalink.

Leave a Reply

Your email address will not be published.