skip to Main Content

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

3

Answers


  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.

    Login or Signup to reply.
  2. 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:

    Clean/Purge Data

    Sorce: link

    Login or Signup to reply.
  3. (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:

    >> wsl --shutdown 
    

    Verify everything is stopped by:

    >> wsl.exe --list --verbose
    

    Then start diskpart:

    >> diskpart
    

    and inside diskpart type:

    DISKPART> select vdisk file="<path to vhdx file>"
    

    For example:

    DISKPART> select vdisk file="C:UsersuserAppDataLocalPackagesCanonicalGroupLimited.Ubuntu22.04LTS_12rqwer1sdgsdaLocalStateext4.vhdx"
    

    it should respond by saying
    DiskPart successfully selected the virtual disk file.

    Then to shrink

    DISKPART> compact vdisk
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search