How to shrink VirtualBox disk

How can a VirtualBox disk be shrunk/compacted to reduce empty space stored inside?

I tried vboxmanage modifyhd [disk] compact and it went through a progress indicator… and then nothing. The disk is the same size as before.

Host is Fedora 30, guest is Windows 7, disk is VDI dynamically allocated storage. I tried the above command before and after shrinking the volume in Windows and leaving the reclaimed space unallocated. No difference.

Also, the disk is on an SSD. Online instructions say to zero empty space in Windows, but that’s not good for SSDs right?

You have to write zero’s into the empty space first before you can use that command.

This site gives a good tutorial:

1 Like

Is that ok on SSDs?

1 Like

The amount you are writing is negligible in the lifespan of you SSD - as long as you don’t do this daily or something like that.

Thanks, after using sdelete, compacting worked.

The nominal disk size is still the same, but the actual size is now reduced. If I try to shrink the disk, the VirtualBox GUI does not allow it, with a message saying “storage shrinking is not currently implemented.” Is that just the GUI or is it impossible to shrink the disk?

Only way I have found is to shrink the partition, make a disk image from the partition, then convert the raw image file to a new .vdi file and then use this disk in Virtualbox.

1 Like

Ok, thanks, I’ll keep that in mind. In this case I’ll skip the hassle, since I expect the disk to need to grow in the future.

It was a feature a few years back. But to many dumb people made their Virtual disks smaller than their data needed, and destroyed servers with that.
VMWare removed that feature a bit later. So no, there is no way to shrink the actual disk size. It’s not just the GUI and doesn’t work over CLI either. And afaik VMWare has no plans to bring that back.

Thanks for the explanation/history @domsch1988 Actually, had it worked, I was wondering if VirtualBox would have prevented shrinking the disk to less than the used space in the VM (I was planning a bit of a buffer)… that answers it! haha