How to Delete VHD File Hyper-V Off Host Machine

When you delete a virtual machine in Hyper-V Manager, you are only deleting the configurations and the virtual hard disk will still take space. So, how to delete VHD file Hyper-V off a host machine completely? In this article, I will introduce 2 ways to do this.

By @Crystal Last Updated September 11, 2024

Case: How to delete VHD file Hyper-V off a host machine?

 

I have created several test VMs and moved a production VM away from a particular host machine. The only remaining VM cannot start due to a lack of disk space.

There should be plenty, the problem is all of the old unused vhd files. But I cannot figure out how to delete them. The Hyper-V Manager only allows me to select them.

- Question from social.technet.microsoft.com

Sometimes you may also find yourself running out of storage space, even though you have already deleted the old Hyper-V virtual machines, as in the case above.

This happens because when you delete a Hyper-V virtual machine in Hyper-V Manager, it only deletes its configuration files, and not delete the virtual hard disks (VHD) associated with the VM. This is a failsafe measure for Hyper-V restore deleted VM.

However, if you want to free your storage space, how to delete VHD file Hyper-V off a host machine? In this article, I will introduce 2 ways to do this. Before you start, please note that this operation will delete the virtual hard disk permanently, you better backup Hyper-V VMs in case you might need to restore some data from it.

Free Agentless Hyper-V Backup Solution

Nowadays it is commonly agreed that backup virtual machines in an agentless and image-level way is better than file-based backup. It is more complete, contains the guest configurations, and independent. You can try AOMEI Cyber Backup – a free Hyper-V backup software to automate your VM protection. More »

Download FreewareVMware ESXi & Hyper-V
Agentless image-level VM backup software

How to delete a VHD file manually in folder

To delete the virtual hard disk of a Hyper-V virtual machine completely, the first thing you need to do is to stop the virtual machine, and delete it in Hyper-V Manager. Or you may encounter the error “The action can’t be completed because the file is open in System”.

1. Launch Hyper-V Manager, right-click on the name of the target virtual machine and select Settings. Click Hard Drive to copy the file path of the target virtual hard disk.

2. Stop the virtual machine, right-click on the name of the target virtual machine, and select Delete… confirm Delete in the pop-up window.

3. Navigate to the folder where the virtual hard disk of the target virtual machine is stored, and delete the target virtual hard disk.

What to do if you still encounter “The action can’t be completed because the file is open in System”

If you still encounter the error “The action can’t be completed because the file is open in System”, shut down the Hyper-V Management Console and click Try Again for more times, or restart the host machine and try again.

How to delete a VHD file completely using PowerShell

Except for manually deleting the VHD file in folder, you can also using PowerShell commands to do the deletion. The first thing you need to do is to find the full path of the target VHD file.

1. Map all of the attributes that are related to the target virtual machine’s HardDrives property to a variable. Set the variable equal to the Get-VM cmdlet and then supply the virtual machine name followed by Select-Object command. The command looks like:

$A = Get-VM | Select-Object -ExpandProperty HardDrives

Note: The command to get the full path of the virtual hard disk:

Get-VM | Select-Object -ExpandProperty HardDrives

When execute this command, the $A variable will contain properties such as the virtual machine name, the controller type, the controller number, the controller location, the disk number, and the path.

2. Then you need to isolate the path by referencing the first variable’s path attribute when assigning the second variable. The command looks like:

$B=$A.Path

3. Stop the virtual machine. And use Remove-Item cmdlet along with the path derived earlier to delete the virtual hard disk. The command looks like:

Remove-Item -Path $B

4. The last thing to do is to delete the target virtual machine itself. You can do this by using the Remove-VM cmdlet, followed by the name of target virtual machine. The command is:

Remove-VM

Summary

When you delete a Hyper-V virtual machine in Hyper-V Manager, what you are doing is only delete the virtual machine from the list, and delete the configurations. The virtual hard disk will still remain on your host machine, and keep taking storage space.

If you want to clean up and make some free space, you may want to compact VHDX or delete the virtual hard disk completely. In this article, I introduced 2 ways of how to delete VHD file Hyper-V off the host machine.

Before you start, please note that this deletion operation is permanent. And in case you may want to restore some data from it, please backup the virtual machine before you delete it permanently. You can try AOMEI Cyber Backup, a free virtual machine backup software that simplifies the process into 3 simple steps.