Reclaim Tons of Disk Space by Compacting Your Docker Desktop WSL 2 VM
We'll go over how to regain lots of disk space by compacting the VM file that Docker Desktop creates. I ended up getting 50gb back.
Last year I made that WSL 2 gotchas video about getting back disk space but it didn’t include shrinking down the Docker Desktop WSL 2 VM itself which is what this new video focuses on. We’ll go over how to prune images with Docker too.
Everything we’ll go over will work for all editions of Windows 10.
# Going Over All of the Steps
Commands
You’ll want to open PowerShell as an admin and then run these commands:
# Close all WSL terminals and run this to fully shut down WSL.
wsl.exe --shutdown
# Replace Nick with your Windows user name. This is where Docker stores its VM file.
cd C:\Users\Nick\AppData\Local\Docker\wsl\data
# Compact the Docker Desktop WSL VM file and you're done.
# NOTE: This may not work with Windows Home edition (read below).
optimize-vhd -Path .\ext4.vhdx -Mode full
If you’re using Windows Home edition and optimize-vhd
isn’t a valid command
you can try running these commands instead:
# Replace Nick with your Windows user name.
diskpart
select vdisk file="C:\Users\Nick\AppData\local\Docker\wsl\data\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
Timestamps
- 0:17 – Vacuuming your Docker Desktop WSL 2 VM disk is really important
- 0:38 – Pruning dangling and maybe old images with
docker system prune
- 2:43 – Launching PowerShell as admin
- 3:12 – Shrinking the Docker Desktop WSL 2 VM for all editions of Windows
- 5:21 – Restarting Docker and opening a regular WSL 2 terminal
- 6:09 – Double checking your disk space to see if it’s reclaimed
Reference Links
- https://nickjanetakis.com/blog/3-gotchas-with-wsl-2-around-disk-space-memory-usage-and-performance
- https://nickjanetakis.com/blog/a-linux-dev-environment-on-windows-with-wsl-2-docker-desktop-and-more
- https://github.com/microsoft/WSL/issues/4699#issuecomment-627133168
How much space did you get back? Let me know below.