Shrink your WSL2 virtual disk on Windows home

I’m running Windows 11 home preview for quite some time. And since then, I’m constantly running out of disk space. I already started to exclude some folders in OneDrive – even if the data only was available online.

I found that I have a lot of docker large images (docker images) that are unused so I removed them:

$ docker system prune --all

But the effect was not noteable. Then I found the post Shrink your WSL2 Virtual Disks and Docker Images and Reclaim Disk Space by Scott Hanselman. The problem: the commandlet optimize-vhd is not available on Windows Home edition! But I found an older post from Stephen that explains how you do it with diskpart.

So here is the combination of both posts:

  1. Go to ~\AppData\Local\Docker\wsl\data and check the file size of ext4.vhdx. Right click the file and select “Copy as path“.
  2. Shutdown wsl and open diskpart:
$ wsl --shutdown
$ diskpart

3. Diskpart open in a new window. Select the disk and compact it:

select vdisk file=<right click to paste complete path to ext4.vhdx>
compact disk

That’s it. It reduced my disk size from 70GB to 7.5GB!

I spent a lot of time researching why Optomize-Vhd is not available. So I thought I document this here for all Windows Home users with WSL2.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s