Why VirtualBox harddisk is always growing even though I delete files from virtual drive?

5,961

Here is a batch script I use to compact all the VDI files:

@echo off
for /d %%d in (*.*) do (
    cd "%%d"
    for %%f in (*.vdi) do (
        echo Compacting %%f . . .
        echo Initial size:      %%~zf
        "C:\Program Files\VirtualBox\VBoxManage.exe" modifymedium --compact disk "%%f"
        echo Post compact size: %%~zf
        echo.
    )
    cd ..
)

Just save it in the folder containing the Virtual Machines and run...

Share:
5,961

Related videos on Youtube

Ajayvignesh
Author by

Ajayvignesh

Updated on September 18, 2022

Comments

  • Ajayvignesh
    Ajayvignesh over 1 year

    I have seen it with both VBox native VDI format and VMDK format. Even though I delete files from virtual drive, I see it always keeps growing. I have some vague idea that file zero-ing doesn't happen with the virtual drives from this link. When I tried to do that with SDelete (as said in the link) it didn't reduce the VDi disk size. I have applications which once installation grows the disk to 10GB each time. I believe there should be some simple way to zero it than going through the tedious process, is there any? Is there any option in VirtualBox to keep virtual drive small?

  • DeathByTensors
    DeathByTensors almost 7 years
    O wow, this is neat! I didn't know this functionality existed
  • Ajayvignesh
    Ajayvignesh almost 7 years
    Should I do SDelete before running this batch file? I see there is no difference between Initial Size and Post Compact Size after running this batch.
  • phuclv
    phuclv almost 7 years
    @Ajayvignesh you must run sdelete or any tools like zerofree to zero the free blocks, because virtualbox/vmware work in raw blocks and have no idea about the underlying filesystem. Hence if it sees a non-zero block, it thinks that the block is has some data and cannot remove it from the file