Dec 13 2014

Why Compacting Hyper-v vhd Does Not Reduce Disk Size

Published by at 12:24 am under Virtualization

You have freed up a lot of space on your hyper-v virtual machine and tried to compact the vhd file but no luck, it doesn’t shrink as much as it should.
 
Here are a few things I tried to optimize disk space:
– Defragmentation
– Shrink volume from the VM disk manager
– Defrag adding the vhd on another VM as a secondary drive so system files can be defragmented
– Generate a new vhd from the original copy
Nothing seems to be working!

The problem is that NTFS deletes files from the directory structure, but does not cleanse space freed up by removed files. And that space isn’t released while compacting. This is why there are a lot of tools able to recover deleted files, as long as other files have not taken released space.
 
In order to get all the free space back, you can use sdelete from the excellent Microsoft sysinternals tools. sdelete lets you securely erase any file data that exists in the unallocated portions of a disk, meaning filling space with zeros. That includes files you have already deleted or encrypted.
Download sdelete and run this short command on the virtual machine if you want to clean the C: drive:

sdelete -z C:


sdelete does not say much, you don’t even know how much space you just saved. it all depends on how much cleanup you have done beforehand.

reduce vhd with sdelete


You can now successfully compact the vhd file which size will match space really used.


 


No responses yet

Trackback URI | Comments RSS

Leave a Reply