Delete shadow copies to compact VHDs and AVHDs

For a few years my primary development machine has actually been a VM with source located on a physical hard drive attached to the VM. This helps reduce latency compared to a differencing disk (AVHD) which may need to expand to accommodate additional data (common when building). It’s also easier to maintain than multiple boot entries when booting to different AVHDs.

But a couple of weeks ago I accidentally created a new source enlistment on my guest system drive – a series of differencing disks on top of a dynamic root disk (VHD). Not only did this expand to the limit of the virtual drive capacity, but left the partition for the host with little extra free space. I needed to compact the AVHD since I already removed the source enlistment.

At least with Hyper-V on Windows Server 2008 R2 you can compact VHDs and AVHDs alike; however, I kept seeing the following error,

The system failed to compact ‘C:UsersPublicPublic DocumentsHyper-VVirtual hard disksdisk.avhd’. Error code: The requested operation could not be completed due to a file system limitation”

I thought perhaps there wasn’t enough drive space remaining on the host partition, assuming VDS may create a new temporary file. I copied it to a partition with a lot more capacity to no avail. Then I found blog post by Jeff Guillet, a Microsoft MVP. Besides a couple of other possible reasons, Jeff had this to say,

You cannot compact a VHD where the VHD contains a VSS backup.

Since you can compact AVHDs as well, I assumed the same may be true for AVHDs. And since my host partition was not heavily fragmented, I gave the suggested solution a try. I booted the guest OS, opened an elevated command prompt, and ran the following,

vssadmin delete shadows /all

When that completed, I shut down the VM and attempted to compact the AVHD again: success! Everything is back up and working and my host partition has space to spare, so I thought I’d share this tip from Jeff in case you run into the same problem.

And to avoid this situation in the future,  also recommend you disable System Restore which will also disable and delete shadow copies. If you’re using differencing disks periodically already, you may not need them anyway and it will save space on the guest partition(s).