I have WSL installed as well as Docker Desktop.
I tried to clean up docker as much as I could by running
docker system prune -a
docker volume rm $(docker volume ls -q -f dangling=true)
Then I verified with
❯ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 0 0 0B 0B
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
However, I see that I still have nearly 12G used by WSL.
I assume this file is docker related. Why is it so large despite me removing all containers and volumes?
How do I shrink it (or can I delete it?)
3
Answers
For reference, there’s an open Github issue on this topic.
WSL2 virtual disks are "dynamic"
.vhdx
‘s, which means they:However, it’s certainly possible to manually optimize them.
Step 0: For others that are reading this, follow the OP’s lead by first cleaning up the Docker data using the normal Docker commands.
Next, in all cases:
wsl --shutdown
from PowerShell or CMDSince you’ve already removed all data (containers, images, volumes), you could just remove the images entirely:
Important: Note that these are destructive operations, so make sure you really don’t need any data from Docker Desktop.
For those that have data that they need to retain, but still need to reclaim some space:
ext.vhdx
as a backup. Note that there have been reports of corruption when using either of the techniques below.Then, as noted in the comments in that Github issue:
On Windows Pro, you can enable the Hyper-V feature and then run the
Optimize-VHD
cmdlet in PowerShell per the original Github issue:If using Windows Home, you’ll need to use use
diskpart
per this comment.Restart Docker Desktop and confirm that it is functional and that all expected data is intact before removing the backed-up
ext4.vhdx
.If you’re willing to wipe all of your docker data, open the Docker Desktop client, click the bug icon in the top bar, and then click Clean/Purge data:
Sorce: link
(Update for December 2022)
The windows utility
diskpart
can now be used to shrink Virtual Hard Disk (vhdx) files provided you freed up the space inside it by deleting any unnecessary files. I found the info in this guide.I am putting the gist of the instructions below for reference but the guide above is more complete.
First make sure all WSL instances are shut down by opening an administrator command window, and typing:
Verify everything is stopped by:
Then start diskpart:
and inside diskpart type:
For example:
it should respond by saying
DiskPart successfully selected the virtual disk file.
Then to shrink
After this the vhdx file should shrink in usage. In my case it went from 40GB to 4GB. You can type
exit
to quit diskpart.