VM Shrink and Optimization Guide

Posted at

After you've installed a clean new Windows 7/8/10,
and installed all the nedded updated from Windows-update,
you'll end up with a hard-drive of about 35GB of a virtual-machine,
with almost nothing on it.

There are several folders you can delete, free-space null'ification and
hard-drive compacting methods that will reduce about 15GB of the hard-file,
without changing anything in your machine configuration itself.

Shrinking the hard-drive will make it easier for you to export VM.

This will also optimize the performances of the VM. :]

  1. Disable system-protection, you don't need it in a virtual-machine,
    where you can use a snapshot when needed.

    Run: C:\Windows\system32\systempropertiesadvanced.exe,
    go to system-protection tab, for each drive click configure,
    set the progress-bar to the minimum possible, and click OK,
    go back and switch to Turn off system protection.
    Click the Delete button (click continue) and do the same for the next drives.
    Reboot the VM once before you'll continue the next steps.

  2. Run Windows disk-cleanup: %windir%\system32\cleanmgr.exe

    select everything, including old Windows-updates, run cleanup.

  3. Clean up leftovers, fix registry, remove old system-restore snapshots with
    this custom CCleaner: https://github.com/eladkarako/mods/tree/store/CCleaner
    download just the CCleaner folder using github-partial-get: https://github.com/eladkarako/github-partial-get/archive/master.zip
    or just download the main exe and the INI file.

  4. Disable the windows update service, and delete C:\Windows\SoftwareDistribution\ to save about 5GB of storage.
    run services.msc, look for the line of Windows Update, double click it,
    click stop, set to disabled, click OK.

    You can now go to C:\Windows\SoftwareDistribution\ and delete its entire-content.
    Don't worry, in-case you'll need the Windows Update again, simply set it to enabled and click run, the folder will be re-created.

  5. Defrag all of the drives with Windows Defrag: %windir%\system32\dfrgui.exe,
    it will make sure the hard-drive content is as continues as possible.

  6. Download sdelete from https://docs.microsoft.com/sysinternals/downloads/sdelete
    open CMD as administrator, and run sdelete c: -z to zero out empty space.

  7. Reboot the VM, and run defrag and sdelete again.

  8. Shutdown the VM in an orderly fasion.

  9. Optional.
    Delete all snapshots of the virtual-machine,
    everytime you'll delete one, its state will merge into the previous one
    (don't worry it won't effect the current state), to save time start by deleting
    the most recent-ones, the ones that are closest to the current-state line.

    This step is need if you wish to export the machine,
    or maybe backup the virtual-machine folder.

  10. My machine name (Windows 7) is w7 and it's located at C:\vm\w7\,
    the hard-drive file name is w7.vdi, make adjustments to where you place your VM,
    and the name of your hard-drive file.

    Open CMD as administrator and run:
    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifymedium --compact c:\vm\w7\w7.vdi

    This is the final-step,
    it will try to trim storage space from the end-image,
    that is why it was so important to defrag and use sdelete.