VM files on physical disk have taken up too much space. How to shrink? [duplicate]

I have a single VM on a physical disk. The physical disk now has 0 free space but inside the guest OS, there is ~50GB free. The guest can start for a short time but then Workstation quickly gives the error message

The operation on .vmdk failed (There is not enough space on the disk). The file system where disk resides is full.

At this point I have to end the session and the VM crashes/shutdown.

I've tried the command vmware-toolbox-cmd disk shrinkonly but it fail due to lack of free space (which I don't understand how is possible).

What can I do?


Solution 1:

There are two phases to shrinking a virtual disk:

  1. Wiping.
  2. Compacting (which confusingly is also referred to as "shrinking").

Wiping fills the guest file system with a zero-filled file to overwrite leftover contents from previously deleted files. This can generate an out-of-space message in the guest, but that's normal and expected.

Compacting removes unused parts of the .vmdk file on the host. Doing this properly depends on the wiping step because that's how it identifies parts that don't need to be preserved. This step requires free disk space on the host because, to avoid data loss on failure, disk compaction will create new disk files. You will need at least as much free space on the host as the size of the largest .vmdk file. (It therefore is advantageous to use split disks, which need much less free space on the host than monolithic disks when shrinking, when defragmenting, or when consolidating snapshots.)

The physical disk now has 0 free space but inside the guest OS, there is ~50GB free.

By itself, that isn't a meaningful statement. Just because the guest OS reports ~50GB free does not mean that shrinking your virtual disk can reclaim 50 GB. For example, you could have a sparse virtual disk with a maximum size of 200 GB of which 150 GB is used, leaving 50 GB "free". But if that virtual disk consumes 160 GB on the host, shrinking would reclaim only 10 GB.


Edit

I didn't notice from your comments that you're using a Windows guest. Windows guests have another, superior option (assuming that you're using VMware Workstation Pro and not VMware Workstation Player): VM > Manage > Clean Up Disks. This is much, much faster than using the traditional shrink approach because it directly examines the guest OS filesystem to identify used and unused parts of the disk and therefore does not need the wiping step. I don't remember offhand if it has the same requirements for free space on the host, but given how much faster it is, any free space requirements it has are likely to be much, much lower.