WSL2 Docker Images Cleanup

19/10/2022

So I have been running my current windows installation for a couple of years, and was starting to run out of disk space on the C-drive as one does after a while.

After a quick inspection with WinDirStat, it was clear that all my space was taken up by ext4.vhdx

As indicated by the path it is used by docker for saving its data.

To get a better understanding how the data is used, run

docker system df
As evident most of my space is spent on build caches – actually this is after I reclaimed 20 GB of disk space used on old images. But I couldn’t understand that my docker files where still taking up so much space.

To reclaim space from the build cache you can use

docker builder prune --filter until=24h

This way you can keep the most recent cache. If you just want to do a deep cleaning it is also possible to use

docker system prune -a

However I did that and it failed to clean the build cache, although it claims it should do that.

This by itself down shrink the size of the vhdx file. To do so you must shutdown WSL wsl --shutdown and run a the following powershell command

optimize-vhd -Path .\ext4.vhdx -Mode full
Ahh much better.