Scott Hanselman

Shrink your WSL2 Virtual Disks and Docker Images and Reclaim Disk Space

October 05, 2021 Comment on this post [8] Posted in Docker | Linux
Sponsored By

Docker Desktop for Windows uses WSL to manage all your images and container files and keeps them in a private virtual hard drive (VHDX) called ext4.vhdx.

It's usually in C:\Users\YOURNAME\AppData\Local\Docker\wsl\data and you can often reclaim some of the space if you've cleaned up (pruned your images, etc) with Optimize-Vhd under an administrator PowerShell shell/prompt.

You'll need to stop Docker Desktop by right clicking on its tray icon and choosing Quit Docker Desktop. Once it's stopped, you'll want to stop all running WSL2 instances with wsl --shutdown

Mine was 47gigs as I use Docker A LOT so when I optimize it from admin PowerShell from the wsl\data folder

optimize-vhd -Path .\ext4.vhdx -Mode full

...it is now 2 gigs smaller. That's nice, but it's not a massive improvement. I can run docker images and see that many are out of date or old. If I'm not using Kubernetes I can turn it off and delete those containers as well from the Docker settings UI.

I'll run docker system prune -a to AGRESSIVELY tidy up. Read about these commands before your try yourself. -a means all unused images, not just dangling ones. Don't delete anything you love or care about. If you're worried, docker system is safer without the -a.

Now my Docker WSL 2 VHD is 15 gigs smaller! Learn more about WSL, Windows 11, and WSLg on my latest YouTube!

NOTE: You can now get WSL from the Windows Store! Go get it here and then run "wsl --install" at your command line.

If you want, you can also go find your Ubuntu and other WSL disks and Compact them as well. I only think about this once or twice a year, so don't consider this a major cleanup thing unless you're really tight on space.

Ubuntu WSL disks will be in folders with names like

C:\Users\scott\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState

or

C:\Users\scott\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState

But you will want to look around for yours. Again, back things up and make sure WSL is shutdown first!

Enjoy! REMEMBER - Be sure to back things up before you run commands as admin from some random person's blog. Have a plan.


Sponsor: YugabyteDB is a distributed SQL database designed for resilience and scale. It is 100% open source, PostgreSQL-compatible, enterprise-grade, and runs across all clouds. Sign up and get a free t-shirt!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 12, 2021 12:21
It seems like optimize-vhd cmdlet not work for windows10 home
October 12, 2021 13:40
Thank you for that interesting post!

In the very first line, you mean WSL instead of SSL, right?
October 12, 2021 23:47
Hi,
Great post, thanks!.
I think you need to install Hyper-V features on Windows for the command to be available. Sadly, Home editions don't have that.
Also, I think there is a typo in
Go get it here and then run "wsl --install" and your command line.

October 13, 2021 4:05
Thanks Benjamin, I will fix that typo.

In fact we worked very hard to make sure that this DOES WORK ON Windows Home! It needs the Virtual Machine Platform, not HyperV client.
October 13, 2021 15:46
hey,

Could you check this website: angularjs development company in usa
October 13, 2021 21:54
Scott, your link to YouTube is broken.
Sam
October 14, 2021 16:31
Hm, I have the Virtual Machine Platform feature installed, but I still don't have the "optimize-vhd" cmdlet on Win11. Does it need something else to be installed as well?
October 14, 2021 21:33
Hi Scott,
I have the Virtual Machine Platform and Windows Hypervisor Platform installed, and I don't have the cmdlet on Windows 11. What I am missing?
Thanks

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.