Disk storage files in wsl2. Where does free space disappear?

Viewed 612

Earlier I used with hyper-v and docker.

Recently I've installed wsl2 for docker usage. After wsl2 installation I dowloaded and installed ubuntu 20 and set it in docker desktop settings.

So command wsl --list returns

-* Ubuntu-20.04           Running         2
-  docker-desktop         Running         2
-  docker-desktop-data    Running         2

I see in daily work that free space disappears on disk C. I found few files with huge size and i'd like to know what it is? And if I could delete them or cut?

Here are the files:

c:\Users\***\AppData\Local\Docker\wsl\data\ext4.vhdx-----------55 gb
c:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx--------45 gb
c:\Users\All Users\DockerDesktop\vm-data\DockerDesktop.vhdx----the same as prev. What is it??

General question - how reduce disk usage? And what are that files?

2 Answers

I observed the same issue: wsl2 got bigger and bigger and even when I deleted files inside of wsl2, I couldn't see the free space reflected on my host machine.

After some research I found a nice step-by-step instruction by Stephen Rees-Carter how to shrink the used space by wsl2: How to Shrink a WSL2 Virtual Disk

A rough explanation:

  • wsl2 is compressed in one single image on your host machine called ext4.vhdx (located somewhere C:\Users\<Username>\AppData\Local\Packages\<Distro>\LocalState)
  • to shrink that file and return unused space diskpart can be used:
    • select vdisk file="pathTo_ext4.vhdx"
    • compact vdisk

In my case the wsl2 image size was reduced by ~30%.

This can be resolved by compacting the vhdx file through diskpart

Below file eats up a lot of diskspace.

C:\Users\xxx\AppData\Local\Docker\wsl\data\ext4.vhdx

Here are the commands to resolve and reclaim your diskspace

  1. Ensure that your docker data is cleaned up
    • docker system prune -all
    • Warning: Deletes all your images, container, volumes and all data that's created by docker
  2. Terminate the wsl dist that's unused.
    • Run the command wsl -l -v and terminate all other entries except the below
      • Entry with * represents your current dist
      • docker-desktop
      • docker-desktop-data
  3. Take the backup of you current dist wsl --export <dist> <path>
  4. diskpart
    1. Opens up a new prompt DISKPART>. Issue the command select vdisk file="your-docker-data-file"
      • Example: select vdisk file="C:\Users\xxx\AppData\Local\Docker\wsl\data"
    2. Finally, do compact vdisk DiskPart successfully compacted the virtual disk file
Related