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

URL: https://www.kemptdesk.com/guides/shrink-wsl-docker-disk
Platform: Mac and PC
Applies to: Windows 10 and 11 with WSL 2; Docker Desktop on Windows and macOS
Reviewed: 2026-11-23
Publisher: KemptDesk (https://www.kemptdesk.com), a disk clean-up app for Mac and PC

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.

## 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](https://www.kemptdesk.com/guides/c-drive-full).

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

| Path | What it is | Safe to delete | What happens if you delete it | How to clean it |
| --- | --- | --- | --- | --- |
| `...\LocalState\ext4.vhdx` | The whole file system of one WSL distribution | No | The distribution and every file, program and setting in it are lost | Delete files inside Linux, then compact the disk |
| `%LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx` | Docker Desktop's images, containers, volumes and build cache | No | Docker loses every image, container and volume | `docker system prune`, then compact the disk |
| `~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw` | The same Docker data on a Mac | No | Docker loses every image, container and volume | `docker system prune`, then check Docker Desktop settings |
| `stopped containers, unused networks, dangling images, build cache` | What `docker system prune` removes | Yes | Stopped containers are gone; the rest comes back when needed | `docker system prune` |
| `images not used by a container` | What `docker system prune -a` also removes | Careful | You pull or build those images again, which needs time and a network | `docker system prune -a` |
| `anonymous volumes not used by a container` | What `--volumes` also removes | Careful | Data stored in those volumes, such as a test database, is lost | `docker system prune --volumes` |
| `wsl --unregister <name>` | A whole WSL distribution you no longer need | Careful | All data, settings and software in it are lost for good | `wsl --export` a backup first, then `wsl --unregister` |

> **Careful:** Deleting `ext4.vhdx`, `docker_data.vhdx` or `Docker.raw` to save space is the same as erasing a disk. Do it only if you accept losing everything in it.

## 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](https://www.kemptdesk.com/guides/developer-caches-node-modules-npm-cargo-gradle). 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
```

3. 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](https://www.kemptdesk.com/guides/mac-storage-full). 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

- [How to manage WSL disk space (Microsoft Learn)](https://learn.microsoft.com/en-us/windows/wsl/disk-space)
- [Basic commands for WSL (Microsoft Learn)](https://learn.microsoft.com/en-us/windows/wsl/basic-commands)
- [Advanced settings configuration in WSL: sparseVhd (Microsoft Learn)](https://learn.microsoft.com/en-us/windows/wsl/wsl-config)
- [compact vdisk (Microsoft Learn)](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/compact-vdisk)
- [attach vdisk (Microsoft Learn)](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/attach-vdisk)
- [detach vdisk (Microsoft Learn)](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/detach-vdisk)
- [Optimize-VHD (Microsoft Learn)](https://learn.microsoft.com/en-us/powershell/module/hyper-v/optimize-vhd)
- [Mount-VHD (Microsoft Learn)](https://learn.microsoft.com/en-us/powershell/module/hyper-v/mount-vhd)
- [Install Hyper-V (Microsoft Learn)](https://learn.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/install-hyper-v)
- [Windows Subsystem for Linux September 2023 update (Microsoft Command Line blog)](https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/)
- [Sparse VHD support is currently disabled (microsoft/WSL issue 13075)](https://github.com/microsoft/WSL/issues/13075)
- [Back up and restore Docker Desktop data (Docker Docs)](https://docs.docker.com/desktop/settings-and-maintenance/backup-and-restore/)
- [docker system prune (Docker Docs)](https://docs.docker.com/reference/cli/docker/system/prune/)
- [Docker Desktop settings: Resources > Advanced (Docker Docs)](https://docs.docker.com/desktop/settings-and-maintenance/settings/)
- [FAQs for Docker Desktop for Mac (Docker Docs)](https://docs.docker.com/desktop/troubleshoot-and-support/faqs/macfaqs/)
- [FAQs for Docker Desktop for Windows (Docker Docs)](https://docs.docker.com/desktop/troubleshoot-and-support/faqs/windowsfaqs/)
- [WSL 3.0.2 adds experimental sparse VHD creation (Phoronix)](https://www.phoronix.com/news/Microsoft-WSL-3.0.2)
