Deleting VHDs in Azure

In Azure you must delete your Virtual Machine so that Azure stops charging you. Just turning off your server is not enough to stop the charging. This is because the CPU is reserved for your Server. To stop the charging you must delete the VM.

image   Click to enlarge

When you delete a Virtual Machine in Azure you delete the reservation for the CPU but that doesn’t delete your data. The data including the OS volume still exist in Azure Storage in VHD files. The configuration of your server also still exists in a service profile. This allows you to bring your server back by creating a “new” server and choosing the old server name. It will bring back your old server.

If you want to get rid of the server entirely you need to delete the VHD file. To delete the VHD file you just need to click on the Disks Menu and then delete your disk. Lastly choose to delete the VHD file or it will stay in your Azure Storage.

image

image Click to enlarge

Sometimes Azure is unable to delete the VHD files because there is still a lease on the file. That is a problem that happens occasionally and what we need to do is break the lease. Unfortunately this currently can not be done from the management portal. Luckily it can be done via our good friend PowerShell. 

How to use PowerShell script to break the Azure lease:

  1. Setup the PowerShell management environment. You can follow my steps here:
    http://mythoughtsonit.com/2013/01/31-days-of-servers-in-the-cloud-setting-up-management/
  2. Download the script BreakLease.ps1 from the link below:
    http://mythoughtsonit.com/wp-content/uploads/2012/11/breaklease.zip
    I extracted the breaklease.ps1 to C:\temp
  3. Remember to run Powershell as Administrator. Make sure the execution policy allows you to run scripts. You can check with the Get-ExecutionPolicy command. You can set the execution policy to allow you to run scripts locally with this command:  Set-ExecutionPolicy Unrestricted
    (Prompt must have admin privileges)

    image

  4. Run BreakLease.ps1 with the URL to the VHD in order to break the lease. The script obtains the necessary storage account information, checks that the blob is not currently registered as a disk or as an image, then proceeds to break the current lease (if any). This URL is found under Storage, <storage name>, Containers, vhds.  You find the URL in the Azure portal as below:
    image
    image

Hope that helps. If you use this to fix your issues – leave me a message and let me know it was useful! –Brian