Shrinking a thin-provisioned VMDK on a datastore with vmkfstools

Written by James McDonald

December 12, 2014

VMotion to another datastore didn’t work:

Just trying to shrink an already thin-provisioned Windows XP VM which is using only 10GB of a 127GB C: drive

I tried to run defrag, sdelete and then shutdown the VM and vmotion to another datastore with a target disk format of thin-provisioned  but this failed to reclaim the free space from the VM. By that I mean that the VMDK still used the same diskspace it originally did when it got to the target Datastore.

Instructions from the web

So next step was following instructions from

Problem when trying to run vmkfstools

After running a defrag on c: and sdelete -z c from inside the running VM and shutting it down to complete the vmkfstools -K [disk].vmdk I got

“Failed to open virtual disk ‘[disk].vmdk’: Failed to lock the file (16392)”

After investigating I found that Backup exec had taken a snapshot of the VM so that it could complete its backup. The fix was to wait till backup exec had completed its backup and removed the snapshot, and therefore the lock it had on the vmdk files.  I could then start the vmkfstools -K [disk].vmdk process

How I did the full process:

  1. Login the VM you want to shrink and defrag the disks
  2. Install sdelete and run sdelete -z C: (repeat for each drive)
  3. Shutdown the VM
  4. Enable SSH on the ESXi host that is hosting the VM and connect via PuTTY
  5. run vim-cmd vmsvc/getallvms and identify the VM ID and Name
  6. run vim-cmd vmsvc/get.datastores <ID>
  7. Using the id get the datastore url
  8. cd /vmfs/volumes/512d4228-3fbc76e4-93cb-d4ae52ba8566/<NAME>
  9. Run vmkfstools -K [disk].vmdk to reclaim the zeroed disks blocks.



Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

List local disks

GET-CimInstance -query "SELECT * from Win32_DiskDrive" DeviceID Caption Partitions Size Model -------- -------...

Create ISO using Powershell

Usage New-IsoFile -NewIsoFilePath C:\tmp\Hyper-V.iso-sources -ImageName Hyper-V -SourceFilePath 'C:\tmp\Hyper-V'...