KemptDesk

Why do WSL and Docker disks keep growing, and how do you shrink them?

For Mac and PCWindows 10 and 11 with WSL 2; Docker Desktop on Windows and macOSReviewed 23 November 20266 min read

Quick answer

A WSL disk (ext4.vhdx) and Docker Desktop's disk (docker_data.vhdx on Windows, Docker.raw on a Mac) grow as you add data but do not shrink when you delete files inside them. Never delete the file itself: it holds the whole Linux system or every image. Prune inside first with docker system prune, then compact the disk.

On this page
  1. Why the disk files keep growing
  2. Where the disk files are
  3. What you can safely remove
  4. Step 1: free space inside Docker and Linux
  5. Step 2: compact the disk on Windows
  6. Docker Desktop on a Mac
  7. How KemptDesk handles this
  8. Questions
  9. Sources

Why the disk files keep growing

WSL 2 keeps each Linux distribution in one virtual hard disk file, ext4.vhdx. Microsoft says WSL "automatically resizes these VHD files to meet storage needs", and each one can grow to a maximum of 1 TB by default. This is often the biggest single file in what fills a Windows C: drive.

Microsoft's diskpart docs say such disks "do not automatically reduce in size when you delete files". So rm inside Linux frees space for Linux, not for Windows.

Docker Desktop works the same way. On Windows with the WSL 2 engine, it keeps images, containers, volumes and build cache in its own .vhdx file, docker_data.vhdx. On a Mac, it keeps them in one disk image file, Docker.raw.

Where the disk files are

  • WSL distribution from the Microsoft Store: %LOCALAPPDATA%\Packages\<PackageFamilyName>\LocalState\ext4.vhdx
  • WSL distribution installed in another way: use the PowerShell command below to find its ext4.vhdx
  • Docker Desktop on Windows: %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx
  • Docker Desktop on a Mac: ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw

To get the exact path of one WSL distribution, Microsoft gives this PowerShell command. Replace <distribution-name> with a name from wsl -l -v:

powershell
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath") + "\ext4.vhdx"

What you can safely remove

PathWhat it isSafe to deleteWhat happens if you delete itHow to clean it
...\LocalState\ext4.vhdxThe whole file system of one WSL distributionNoThe distribution and every file, program and setting in it are lostDelete files inside Linux, then compact the disk
%LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdxDocker Desktop's images, containers, volumes and build cacheNoDocker loses every image, container and volumedocker system prune, then compact the disk
~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.rawThe same Docker data on a MacNoDocker loses every image, container and volumedocker system prune, then check Docker Desktop settings
stopped containers, unused networks, dangling images, build cacheWhat docker system prune removesYesStopped containers are gone; the rest comes back when neededdocker system prune
images not used by a containerWhat docker system prune -a also removesCarefulYou pull or build those images again, which needs time and a networkdocker system prune -a
anonymous volumes not used by a containerWhat --volumes also removesCarefulData stored in those volumes, such as a test database, is lostdocker system prune --volumes
wsl --unregister <name>A whole WSL distribution you no longer needCarefulAll data, settings and software in it are lost for goodwsl --export a backup first, then wsl --unregister

Step 1: free space inside Docker and Linux

Docker's docs say docker system prune removes "all stopped containers", "all networks not used by at least one container", "all dangling images" and "unused build cache". It asks before it deletes:

bash
docker system prune

Add -a to also remove every image that no container uses. Add --volumes to also remove anonymous volumes that no container uses. Volumes you named are not removed.

bash
docker system prune -a

In a WSL distribution, delete what you do not need inside Linux; see which developer caches are safe to delete. This Microsoft command shows how full it is:

powershell
wsl.exe --system -d <distribution-name> df -h /mnt/wslg/distro

Step 2: compact the disk on Windows

Compacting gives free space back to Windows and deletes no files inside Linux. Save your work and quit Docker Desktop first.

With diskpart (every Windows edition)

  1. Open PowerShell as administrator.
  2. Stop WSL. This ends every running distribution:
powershell
wsl --shutdown
  1. Start diskpart and run these lines one by one, with your own .vhdx path:
cmd
diskpart
select vdisk file="C:\Users\you\AppData\Local\Packages\<PackageFamilyName>\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

Microsoft's docs say you can only compact a disk that is "detached or attached as read-only".

With Optimize-VHD (Windows Pro or Enterprise with Hyper-V)

Optimize-VHD is in the Hyper-V module, and Hyper-V "can't be installed on Windows 10 Home or Windows 11 Home". In an administrator PowerShell window:

powershell
wsl --shutdown
Mount-VHD -Path "C:\path\to\ext4.vhdx" -ReadOnly
Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode Full
Dismount-VHD -Path "C:\path\to\ext4.vhdx"

Full mode needs the disk mounted read-only. The compact "can succeed without reducing the file size, if no optimization is possible".

The sparse VHD setting

In September 2023, Microsoft added a way to make a WSL disk shrink on its own:

powershell
wsl --manage <distro> --set-sparse true

Later WSL releases turned it off. On WSL 2.5.8, the command prints "Sparse VHD support is currently disabled due to potential data corruption" and asks for --allow-unsafe. WSL 3.0.2, a pre-release from October 2026, brings sparse disks back as an experimental feature. Run wsl --export first if you try it.

Docker Desktop on a Mac

On a Mac, Docker.raw is often one of the big items in what fills Mac storage. Run docker system prune first. Then open Docker Desktop, go to Settings > Resources > Advanced, and check two settings:

  1. Disk usage limit: "the maximum amount of disk space the engine can use". A lower limit caps how big the disk can grow.
  2. Disk image location: move the disk image to another disk if your startup disk is full.

Before you lower the limit a lot, back up the volumes you need.

How KemptDesk handles this

On a PC, Hidden space has a Linux disks (WSL) section with one card per distribution, its .vhdx path and its size. Compact disk opens an administrator PowerShell window that runs fstrim inside Linux, wsl --shutdown and a read-only diskpart compact vdisk.

Back up runs wsl --export. Remove runs wsl --unregister only after you type the name. Docker Desktop's distributions get a Docker Desktop tag and only Compact disk.

KemptDesk marks .vhdx files and Docker.raw as Breaks an app (red) and never selects them. It does not turn on sparse mode and does not run docker system prune. On a Mac, KemptDesk explains Docker.raw but does not compact it.

Questions

Why is ext4.vhdx so big after I deleted files?

The .vhdx file grows when Linux writes data, but it does not shrink when Linux deletes it. Compact it with diskpart or Optimize-VHD after wsl --shutdown to give the space back to Windows.

Can I delete ext4.vhdx?

Only if you accept losing the whole distribution. To remove a distribution on purpose, run wsl --export for a backup, then wsl --unregister; never delete the file while WSL knows about it.

What does docker system prune delete?

Stopped containers, networks no container uses, dangling images and unused build cache. With -a it also removes images no container uses; with --volumes it also removes anonymous volumes no container uses.

Sources

See what is safe to delete on your computer

KemptDesk scans your disk, explains every folder and selects only what apps can rebuild. The scan is free and cleans up to 500 MB. No account.

Then pay once from $59 or yearly from $9.99 for the first year until 31 December 2026, then $24.99. One license covers Macs and PCs. See plans