How to shrink a VHD or VHDX file
Virtual Disk Trim product page & download
A dynamically expanding VHD or VHDX grows as the virtual machine writes to it and never shrinks by itself. Deleting files in the guest frees space in the guest only, while the file on the host stays as large as it was. To get the space back, the image has to be compacted. This guide shows the built-in way of compacting VHD and VHDX, and a simpler way with Virtual Disk Trim.
Built-in way: Optimize-VHD
Shut the virtual machine down, then in an elevated PowerShell on the Hyper-V host do the following:
Optimize-VHD -Path D:\VMs\server.vhdx -Mode Full
Dismount-VHD -Path D:\VMs\server.vhdx
The read-only mount is important: only then can Windows read the guest file system and see which blocks are free. Edit Disk → Compact in Hyper-V Manager does the same job.
When a disk image does not shrink
- The guest is Linux. Windows cannot read ext4, XFS or btrfs, so it only releases blocks that are zeroed or, for VHDX, trimmed. Start the guest and run fstrim for VHDX, or zero the free space first.
- The disk was not mounted read-only, so the free space could not be determined.
- The VM has checkpoints. Its current data is in .avhdx or .avhd files. You need to delete the checkpoints and let them merge first.
- The disk is fixed-size. Optimize-VHD does not compact fixed disks. You need to convert it to dynamic with Convert-VHD.
- There is no Hyper-V. The cmdlets come with the Hyper-V role or feature, which Windows Home lacks, and do not exist on macOS or Linux. On Windows without Hyper-V, compact vdisk in diskpart is the fallback.
Simpler way: Virtual Disk Trim
Virtual Disk Trim reads the guest file system from the image file itself and rebuilds the image without the unused blocks. It needs no Hyper-V and runs on Windows, macOS and Linux.
- Shut the virtual machine down.
- Add the VHD or VHDX, or the folder with all your disk images. The Savings column shows how much space each image could give back.
- Press Trim. The image is trimmed, verified against the original, and only then replaces it.

Or from the command line:
Which to choose
For a Windows guest on a Hyper-V host, the free Optimize-VHD gives about the same result. Virtual Disk Trim is the better choice when:
- the guest runs Linux — ext4, XFS, btrfs and LVM are read directly, with nothing to run inside the guest;
- the image is not on a Hyper-V host — a backup, an archive, a template, a disk on a NAS or a Mac;
- the disk is fixed-size, and you want it to become dynamic while eliminating its empty space;
- you want to leave out the page and hibernation files as well;
- you have many images, or VMDK, VDI and qcow2 files alongside the VHD and VHDX ones.
Virtual Disk Trim does not trim checkpoint disks or the base disk under them: delete the checkpoints and let them merge first. Neither tool reduces the capacity the guest sees.
Moving to another hypervisor? The vdtrim command line tool can trim and convert the image in one pass; the extension of the output image file determines its format:
Shrink, compact or compress?
Hyper-V calls this operation compacting, and shrinking a VHD or VHDX usually means the same thing: dropping the blocks the guest no longer uses. Compressing is different. It packs the data itself, as NTFS compression of the file on the host does, which saves space at some cost in speed. Reducing the capacity the guest sees is a third thing: the partition has to be shrunk inside the guest and the disk resized afterwards, which Hyper-V does for VHDX only.