How to find orphaned VHD files in the Azure IaaS cloud

Cover

When you delete a VM via Microsoft Azure portal, you don’t have an option to delete the VM related objects (virtual network interfaces and virtual disks as well) Emoj.

Orphaned virtual disks – these are *.vhd files that reside on your StorageAccount(s) and consume expensive storage space, but are not related to any VM.

As always, the Mr. PowerShell will help us in this task. I will also show you a method to remove/delete these disks!

Introduce to you my next function Get-AzOrphanedVhd from my Azure Automation Az-Module which will find all your orphaned disks in all ResourceGroups on all StorageAccounts.

Get-AzOrphanedVhd

  • This simple function has no parameters at all!

  • All you need to start using it, is to login to your Azure account, select your target subscription and just run the Get-AzOrphanedVhd.

Login-AzureRmAccount
Select-AzureRmSubscription
Get-AzOrphanedVhd
  • You can skip the first two steps if you are using my New-AzCredProfile function. Open PowerShell console and run the function. Can it be even simpler?
Get-AzOrphanedVhd

01.Get-AzOrphanedVhd

Returned objects

  • All properties, returned by the function are self-explained. Two properties only I want to note.
Property Description
LastWriteDays Days gone since the disk was modified
Modified Disk modification date in your! local time
  • Two environment variables $WarningPreference and $ErrorActionPreference are set to SilentlyContinue to hide these kind of warnings and error messages.

02.Get-AzOrphanedVhd_Warning03.Get-AzOrphanedVhd_Error

Orphaned does not mean needless

  • Do remember! Even if a virtual disk is not related to any VM, this does not mean there is no valuable data on it. Think twice before you delete any virtual disk.

  • If you still have decided to delete some virtual disk(s), you can do it either through Azure portal UI or Remove-AzureStorageBlob cmdlet.

Get-Help Remove-AzureStorageBlob –Full
Get-Help Get-AzureStorageBlob –Full
  • This example with Select-AzObject helper function, described in this article, will remove a single orphaned disk.
Get-AzOrphanedVhd
VHD : vm1.vhd
StorageAccount : teststg001lrsp
…

Select-AzObject SA | Get-AzureStorageContainer | Get-AzureStorageBlob -Blob vm1.vhd | Remove-AzureStorageBlob -Confirm:$true
  • Here vm1.vhd is orphaned VHD, returned by Get-AzOrphanedVhd that you want to delete.

  • Choice from menu the StorageAccount, returned by Get-AzOrphanedVhd in the StorageAccount property for your disk.

  • To avoid accidental disk removing always use the Remove-AzureStorageBlob cmdlet together with -Confirm:$true parameter.

Summary

  • For more details about the function, please take a look at the content based help and examples.

Get-Help Get-AzOrphanedVhd -Full
Get-Help Get-AzOrphanedVhd -Examples

You may also like:

Azure Automation PowerShell Az-Module Home
Azure Automation – How to stop/start Azure VM on schedule
Azure VM Tag automation
How to login to the Azure automatically

2 thoughts on “How to find orphaned VHD files in the Azure IaaS cloud

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s