Skip to content

Howto Shrink a Thin Provisioned Virtual Disk (VMDK)

Thin provisioned disks are a great feature to save capacity as you virtual machines filesystem will never use the full capacity. I do not know a single system where you do not have at least 10GB of free space for OS disks. I am not considering databases, applications or fileservers which will grow constantly. Having thin provisioned disk is usually no longer a performance problem so it is a valid design choice even in production.

A common issue with thin disks is that the size will grow when required, but never shrink. When you require the capacity only once you might want to get it back from the virtual machine. This post describes how to reclaim unused space from the virtual machine.

Virtual Machine Preparation (Windows)

Windows does not automatically zero deleted blocks. Microsoft provides a tool that can zero blocks after while deleting a file or zero out the entire free space. This is required to reclaim space back from the virtual disk.

  1. Download SDelete
  2. run sdelete.exe -z [Drive]
    sdelete

Wait a couple of minutes until the process is finished. Please note that you virtual disk file (VMDK) will grow to the full size during the process.

Virtual Machine Preparation (Linux)

Linux does not zero deleted blocks too. There are various tools available to create zeroed blocks. The best known tool is dd which should be available on all systems.

  1. Identify free space with df
  2. Fill the free space with dd
vma:/mnt/data # df -h
vma:/mnt/data # dd bs=1M count=8192 if=/dev/zero of=zero

This will zero 8GB of the available 8.2GB (1MB Blocksize * 8192 = 8GB). Please note that you virtual disk file (VMDK) will grow to the full size during the process.dd-fill-free-space-with-zero

Shrink VMDK File

  1. Power off the Virtual Machine, or disconnect the virtual Disk you want to shrink
  2. Connect to the ESXi Host with SSH
  3. Navigate to the Virtual Machine Folder
  4. Verify disk usage with du
  5. Run vmkfstools -K [disk]
  6. Verify disk usage with du
root@esx3:/vmfs/volumes/ds1/vma $ du -h vma_1-flat.vmdk
7.9G vma_1-flat.vmdk

root@esx3:/vmfs/volumes/ds1/vma $ vmkfstools -K vma_1.vmdk
vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
Hole Punching: 25% done.

root@esx3:/vmfs/volumes/ds1/vma $ du -h vma_1-flat.vmdk
1.9G vma_1-flat.vmdk

 

26 thoughts on “Howto Shrink a Thin Provisioned Virtual Disk (VMDK)”

  1. Excellent post! I am going to do this on a volume that is 1.2TB - any ideas how long that will take? I need to build a communication for the environment on how long the VM will be down. Thanks!

  2. Hi, Josh.
    You can do it completely without downtime with Extrasphere disks (not VM) migration to another datastore even on free licensed ESXi host.

  3. Thanks for sharing this useful info. I have a question after applying these steps. flat.vmdk size has been decreased but VM Provisioned Storage is still showing the same size as original flat.vmdk. Please let me know how I can reduce this size as well? Any help is highly appreciable. Than You.

    1. Once you migrate to another host the provisioned storage reflects the shrunk size (so migrate to another host and back again). Note: without vMotion you'll need to have the VM powered off for the duration.

    2. Provisioned storage is the amount of space the disk can grow to. So it's correct that it does not decrease when shrinking disks.

      1. Sorry, typo. The actual VM "Storage Usage" size is properly reflected in the vSphere UI. Provisioned size obviously does not change.

  4. Important addition regarding the Linux preparation step: the "dd" command creates a file named "zero" inside the current directory with the given size (8GB in your example) because of the parameter "of=zero". Therefore not only the virtual disk (VMDK) grows up but also the virtual drive inside the guest gets used nearly 100%. So you should mention that the file "zero" must be deleted after shrinking to free up the filled space inside the guest!

      1. Thanks for your reply. How do you know this? It's not in VMware's own documentation on that page I linked. Can you please provide a source?

        Thanks
        Raimond

        1. It is a common notation that the short and a long command is in the documentation and in manpages. Using both does not work:


          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools -K --punchzero pixel.vmdk
          Failed to open virtual disk '--punchzero': The system cannot find the file specified (25)
          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools --punchzero pixel.vmdk
          vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
          Hole Punching: 0% done.
          [root@esx4:/vmfs/volumes/57604a1d/pixel] vmkfstools -K pixel.vmdk
          vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
          Hole Punching: 0% done.

  5. Does the required steps require you to have the same amount of space available on the datastore already to complete this? Our datastore has no space and we have no place to move the 2TB VMDK file. So would we need 2TB free on the datastore to do this to reduce the space?

  6. I've tried this on a server I want to shrink in size. I used SDelete -z to zero out the drives which increases the used storage to the full size of the disks but now when I run vmkfstools -K on the vmdk files it completes but they do not get any smaller and I am stuck with a server that is consuming almost 1TB on disk when it only has about 100GB of data on it's drives. Any idea why the vmkfstools command isn't working for me? Using ESXi 6.0 and all the drives are thin provisioned.

    1. Oh nvm my last post, I was using ls -l to view the files but that shows the full provisioned size. du -h *.vmdk shows the actual disk usage size which was being shrunken correctly.

  7. Hi,

    hope somebody can help.
    Is there any way to check if a Virtual Disk (VMware) was used or teated with "sdelete.exe -z" ?

    I can not remember on which VMs i executed "sdelete.exe -z"

    I would like to shrink the Virtual Disks because I need to safe as much space as possible on my Storage where the VMs are running.

    Can somebody help?

    Thank you :-)

  8. Hi,

    Is it possible to reduce the disk size to a specified size as i dont see any option to reduce to specific size.

  9. Thank you for this article. That was very helpful for me and solve my problem. PS: This procedure do not shrink the VMDK provisioned size, only the VMDK file size.

  10. Somehow, this didn't work...

    It's a Linux-VM with a thin-disk of 8GB. In use are ...GB.

    On the ESX 6.0u3 I can see it has 8GB:
    8589934592 Jan 16 11:10 SLES-WEBSVR-flat.vmdk

    After zeroing:
    #> dd if=/dev/zero of=/test.dat bs=4k
    #> rm /test.dat
    #> poweroff

    I shrink the disk:
    #> vmkfstools -K SLES-WEBSVR.vmdk
    vmfsDisk: 1, rdmDisk: 0, blockSize: 1048576
    Hole Punching: 100% done.

    And it's the same size.

    I'd welcome any thoughts and hints!

    cheers!

Leave a Reply

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