'Docker Desktop WSL ext4.vhdx too large

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?)

enter image description here



Solution 1:[1]

For reference, there's an open Github issue on this topic.

WSL2 virtual disks are "dynamic" .vhdx's, which means they:

  • Are allocated to a maximum size
  • Are initialized with just a few kilobytes of structural data
  • Grow dynamically as data is added, up to their maximum allocated size
  • And here's the kicker -- They do not automatically shrink when data is removed.

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:

  • Stop Docker Desktop
  • wsl --shutdown from PowerShell or CMD

Since you've already removed all data (containers, images, volumes), you could just remove the images entirely:

  • Uninstall Docker Desktop
  • From PowerShell:
    wsl -l -v
    # If they still exist, remove via ...
    wsl --unregister docker-desktop
    wsl --unregister docker-desktop-data
    
    Important: Note that these are destructive operations, so make sure you really don't need any data from Docker Desktop.
  • Reinstall Docker Desktop

For those that have data that they need to retain, but still need to reclaim some space:

  • Copy the 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:

    Optimize-VHD -Path .\ext4.vhdx -Mode full
    
  • 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.

Solution 2:[2]

If you're willing to wipe all of your docker data, open the Docker Desktop client, click the bug, and then click Clean/Purge data:

Clean/Purge Data

Sorce: link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NotTheDr01ds
Solution 2 Zags