106

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

K Split X
  • 3,405
  • 7
  • 24
  • 49

4 Answers4

109

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

Zags
  • 37,389
  • 14
  • 105
  • 140
90

(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:\Users\user\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_12rqwer1sdgsda\LocalState\ext4.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.

ScrabbleCy
  • 1,001
  • 7
  • 3
  • I think a shrinking by factor 10 is rather unlikely, for me I was able to reduce 55GB to 48GB. – carlo711 Feb 12 '23 at 17:18
  • Thanks alot bro! Mine from 107.9GB to 45.9GB – David Truong Mar 05 '23 at 08:35
  • Thanks! 141.8GB to 137GB – Omeriko Mar 27 '23 at 23:43
  • 1
    Nice! From 37GB to 17GB. I think the amount of disk space that you save depend on the number of images that you had. For example I had alot of images that were taking alot of space, but after deleting the images in docker desktop I couldn't reclaim that space because .vhdx can't shrink down automatically. But after this, the size of dynamically allocated .vhdx reduces. – Moiz Hassan May 30 '23 at 11:26
  • Strangely, this did not free up any space. The file size is currently 7GB. Any other solutions? – qwertynik Jun 22 '23 at 08:41
  • If it did not free up any space, is ir likely that there are actual files taking up that space ? You can use a tool like `ncdu` to examine the usage of disk space in linux. Using `ncdu -x /` will give you a full breakdown of usage across the whole drive without crossing into another mounted filesystem. – ScrabbleCy Jul 28 '23 at 10:46
  • Awesome! 503GB to 151GB. For context I downloaded lots of LLMs but then moved them to another drive, so running this command shrunk the `ext4.vhdx` down to what was actually being used by WSL (confirmed with `df -h`). – matt Aug 14 '23 at 02:36
  • This works great (128GB -> 30GB)! Remember to first remove unused or dangling images, unused volumes and clear the build cache. The "Disk usage" extension can be useful for that. – jlh Aug 18 '23 at 15:49
51

Short answer:

  • diskpart or the Optimize-VHD commandlet can be used to shrink the virtual disk. Optimize-VHD is typically preferred, but is only available under Windows Pro or higher. See below for details.

More Details:

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.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Thank you, this helps. Was able to reduce to 5.5GB. The Windows Home applied to me as Hyper V is not supported on Windows Home, so the alternate worked. Thank you so much. Although 5GB still seems like quite a bit. Any way to increase it more? – K Split X Feb 01 '22 at 23:32
  • @KSplitX That's higher than I would expect as well. I don't think I have anything critical in mine at the moment. Let me see if I can clean it up, try optimizing mine, see what the size comes out to. – NotTheDr01ds Feb 02 '22 at 00:34
  • 1
    it worked for me but after a quick restart. – Bathri Nathan Apr 21 '22 at 10:49
  • it worked, to complement: this article helped me a lot about export/import/resizing/compact, and also, move the location: https://levelup.gitconnected.com/how-to-reduce-size-of-docker-data-volume-in-docker-desktop-for-windows-c9545984bae4 – João Carlos Brasileiro May 23 '23 at 02:21
0

From within a Linux VM (be it the Docker-provided or any other you installed yourself), you can run sudo fstrim -a (or sudo fstrim -av if you want details) and let the hypervisor deallocate the empty blocks of the virtual disk. This is managed by the hypervisor itself and works on KVM and Hyper-V (and, most likely, any self-respecting hypervisor).

rbanffy
  • 2,319
  • 1
  • 16
  • 27