Skip to content

Instantly share code, notes, and snippets.

@GorlikItsMe
Last active November 19, 2025 18:53
Show Gist options
  • Select an option

  • Save GorlikItsMe/9b15d565c68ea2b2d030981215cc6a04 to your computer and use it in GitHub Desktop.

Select an option

Save GorlikItsMe/9b15d565c68ea2b2d030981215cc6a04 to your computer and use it in GitHub Desktop.

How to shrink docker_data.vhdx

Previous tutorials didnt worked for me because my setup is a little bit odd.
I'm using windows 11 machine with WSL2 and docker desktop.
I also use k3d to create kubernetes cluster. Also my k3d cluster have local registry
I also used skaffold and other shinigens

0. How much garbage you have?

%USERPROFILE%\AppData\Local\Docker\wsl\disk\ - there is your docker_data.vhdx %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ - there is your WSL Ubuntu Check both files size, in my case docker_data.vhdx had 250GB

1. Cleanup docker

First try do "normal stuff" like going into docker, delete not used volumes, images, buildcache etc. You can delete this using Docker Desktop or like a hacker run this:

docker system prune -a

This will delete all unused images not just dangling ones.

2. First drive shrink

After this You can check how many data you was able to restore. In normal cases this is enough.

  1. Stop Docker desktop (and wait for it to fully close)
  2. Stop wsl wsl --shutdown
  3. Check if wsl is closed wsl --list --running (if not, shutdown again) I often forgot about opened vscode that is connected to WSL. Close it too

Now we can shrink drive!

Open powershell as administartor sudo powershell

optimize-vhd C:\Users\USER\AppData\Local\Docker\wsl\disk\docker_data.vhdx -Mode Full
optimize-vhd C:\Users\USER\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx -Mode Full

Perfect you shrinked your drives successfuly!

3. Start of the haos

But in my case that didnt worked. Why? because when you use k3d you have another docker inside k3d. You only removed stuff from "outside" docker. We need to delete stuff that is inside!

4. k3d - registry cleanup

Open Docker desktop and find registry.localhost (or similar container created by k3d).

Open terminal of that container and run:

registry garbage-collect /etc/docker/registry/config.yml -m

That will delete all images without tags. In my case it left only :latest images.

Perfect, that saved me 60GB

5. k3d - server-0 cache (or desktop-control-plane)

Do you remmember that I said that your k3d have inside another docker? It lives there inside k3d-{...}-server-0 .

Open Docker desktop and find that container, open terminal ad run:

crictl rmi --prune

That will remove all build cache, dangling images etc. For me it removed old images and buildcache.

That way I saved 120GB

6. Final shrink

After this shinigens above you can finaly try shrink drives.

Remmember to stop Docker Desktop, stop wsl etc.

sudo powershell
wsl --shutdown
wsl --list --running

optimize-vhd %USERPROFILE%\AppData\Local\Docker\wsl\disk\docker_data.vhdx -Mode Full

And finaly it went down from 250GB to 95GB where I have only stuff that I need. Also I dont need to recreate whole cluster :-)


Extra

How to find what is taking place?

This 2 tools saved me a lot time

@drobin04
Copy link

For windows, WizTree is really nice in comparison to WinDirStat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment