Why compact VHDX file?
It is recognized that Hyper-V dynamically expanding disks are convenient for they can grow as needed. Compares to fixed virtual hard disks, it provides better space utilization, however at the same time, more disk fragmentation.
In fact, once the dynamically expanding disks have grown, they will never free up any space unless you manually compact or shrink it. If you delete files from a dynamically expanding disk, you are only freeing up space within the virtual hard drive, instead of the physical VHDX file.
Therefore, to better work with Hyper-V dynamically expanding disks, here I will introduce 2 efficient ways to compact VHDX files. Before you start, to avoid unexpected data loss, please remember to back up Hyper-V VMs in advance.
What is the difference between compact and shrink VHDX
If your VM storage capacity is not enough, you can expand Hyper-V disks. But when a virtual hard disk expands to its maximum size, thus consuming a lot of physical storage space, there are 2 common options to choose from: Compact and Shrink.
● Compact VHDX: Reduces the physical size, but not the logical size of the VHDX file. It only compacts the file size of a VHDX.
● Shrink VHDX: Reduces the logical size of the VHDX file. It reduces the total storage capacity of the VHDX.
You should open the Edit Disk wizard in Hyper-V Manager to shrink a virtual disk in Hyper-V.
Important: backup Hyper-V VMs to avoid accidental data loss
Before you start cleaning the virtual machine, taking backups of your VM data is a reliable insurance to avoid unexpected data loss.
Here I recommend you a professional backup software, AOMEI Cyber Backup, for it has the following benefits.
✦ Concise Steps: With 3 easy steps you can quickly create a complete backup task.
✦ Multiple VMs Backup: You can cover multiple or even all VMs on the host in one backup task.
✦ Offsite Storage: You can back up your VMs to network storages for better protection.
✦ Offsite Restore: You can restore the backup to new VMs on nother host.
✦ Supported Versions: Hyper-V in Windows 8/8.1/10/11, Windows Server/Microsoft Hyper-V Server 2012 R2 and later versions.
Next, I will show how to quickly create a Hyper-V VM backup task. You can click the following button to start a free trial.
2 easy steps to backup Hyper-V VM
1. Launch AOMEI Cyber Backup, navigate to Backup Task and click + Create New Task. Set the Task Name, Device, Target, Schedule, and Cleanup as needed.
2. Now you can click Start Backup and select Add the schedule and start backup now, or Add the schedule only.
How to compact VHDX file via Hyper-V Manager GUI
1. Launch Hyper-V Manager, power off the target VM and right-click on its name, select Settings.
2. Navigate to Hard Drive from the left inventory, Click Edit button to open an Edit Virtual Hard Disk Wizard.
3. Click Next to skip the previous steps and go to the Choose Action tab, select Compact option and click Next to continue.
4. Review the Summary and click Finish to close the wizard.
How to compact VHDX file via PowerShell
Power off the target Hyper-V VM, launch Windows PowerShell, and run the following commands to compact VHDX:
Mount-VHD -Path ‘vhdx folderpath\vhdxname.vhdx’ -ReadOnly
Optimize-VHD -Path ‘vhdx folderpath\vhdxname.vhdx’ -Mode Full
Dismount-VHD ‘vhdx folderpath\vhdxname.vhdx’
Compact VHDX is a common way to free up space while using Hyper-V dynamically expanding disks. In this article, I introduced how to compact VHDX via Hyper-V Manager GUI and PowerShell in detail. Meanwhile, in Hyper-V VM, shrinking a disk is also efficient to save storage space.
When deleting files or cleaning disks, there is always a risk of data loss. Therefore, a proper backup schedule is of great importance, especially for enterprises. Here I recommend you AOMEI Cyber Backup, which allows you to assign sub-accounts with limited privileges, effectively avoids mis-operations.