← Journal

How to reclaim disk space from Docker Desktop and WSL on Windows

Docker and WSL store everything in virtual disks that grow but never shrink on their own. Here is how to prune Docker safely, clean up inside Linux, and compact the .vhdx files to get the space back on Windows 10 and 11.

Terminal commands that prune Docker, shut down WSL and compact the virtual disk
guideBy: FreeMyDisk TeamPublished: Read: 9 min

If you develop on Windows with Docker Desktop or WSL, there is a good chance they use more of your drive than anything else. Images, build layers, containers, Linux packages and project files all live inside virtual disks, and those disks have one awkward property: they grow, but they never shrink on their own.

This guide shows how to free space inside Docker and WSL, and then how to compact the virtual disk files so Windows actually gets that space back.

The short answer First free space inside: docker system prune and docker builder prune for Docker, and sudo apt clean plus removing old files inside WSL. Then run wsl --shutdown and compact the virtual disk with Optimize-VHD (Windows Pro) or diskpart (any edition). Both steps are needed: pruning alone doesn’t shrink the file.

The whole process at a glance

  1. Measure: find the .vhdx files and run docker system df.
  2. Prune Docker: docker system prune and docker builder prune. Keep volumes unless you have backed them up.
  3. Clean inside WSL: package caches, old projects and dependency folders.
  4. Shut down: quit Docker Desktop and run wsl --shutdown.
  5. Compact: Optimize-VHD on Windows Pro, or diskpart on any edition.
  6. Prevent: cap Docker’s disk size and move big disks to another drive.

The rest of this guide walks through each step, with the exact commands and the mistakes to avoid.

Why the space doesn’t come back

Docker Desktop and every WSL 2 distribution keep their files in a .vhdx virtual disk on your Windows drive. When you pull an image or install a package, the disk file grows to make room. When you delete something, Linux marks the space as free inside the disk, but the .vhdx file on Windows keeps its size.

Deleting inside Linux frees space inside the vhdx, but compacting is what returns it to Windows

That is why people run docker system prune, see “Total reclaimed space: 12 GB”, and then find the same 12 GB still missing from their C: drive.

Step 1: Find the virtual disks and their size

Run this in PowerShell to list every .vhdx under your user’s local app data, largest first:

Get-ChildItem "$env:LOCALAPPDATA" -Recurse -Filter *.vhdx -ErrorAction SilentlyContinue |
  Sort-Object Length -Descending |
  Select-Object @{n='GB';e={[math]::Round($_.Length / 1GB, 1)}}, FullName

The usual suspects:

Virtual disk Typical location
Docker Desktop data %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx (older: Docker\wsl\data\ext4.vhdx)
WSL distro from the Microsoft Store %LOCALAPPDATA%\Packages\<distro package>\LocalState\ext4.vhdx
WSL distro installed with wsl --install %LOCALAPPDATA%\wsl\<id>\ext4.vhdx on recent versions
Podman or Rancher Desktop Their own folders under %LOCALAPPDATA%

Step 2: Free space inside Docker

Start by seeing where Docker’s space goes:

docker system df

It shows images, containers, local volumes and build cache, with how much of each is reclaimable. Then choose how far to go:

Docker prune commands from safe to destructive

For most people, this pair is the safe, regular cleanup:

docker system prune
docker builder prune

They remove stopped containers, unused networks, dangling images and the build cache. Your running containers, tagged images and volumes stay.

Go further only when you mean to:

  • docker system prune -a removes every image not used by a container. You will download them again the next time you need them.
  • docker volume prune removes volumes no container uses. Volumes hold databases and other persistent data. List them with docker volume ls and check before you prune.

Back up a volume before you remove it

If a volume holds data you might want later, such as a local database, save it to a file first. This command copies the volume myvolume into myvolume.tgz in the current folder, using a tiny temporary container:

docker run --rm -v myvolume:/data -v "${PWD}:/backup" alpine tar czf /backup/myvolume.tgz -C /data .

To restore it later into a new volume, run the same command the other way round, with tar xzf instead of tar czf. Once you have the backup, removing the volume is no longer a risk.

Careful Docker Desktop’s Troubleshoot › Clean / Purge data deletes everything: images, containers and volumes. It is a factory reset, not a cleanup.

Step 3: Free space inside WSL

Inside each Linux distribution, clean the usual places. For Ubuntu and Debian:

sudo apt clean          # remove downloaded .deb packages
sudo apt autoremove     # remove packages nothing needs any more
du -sh ~/.cache         # check the size of your cache folder
rm -rf ~/.cache/pip ~/.cache/yarn   # clear tool caches you don't need

Also check your home folder for old projects, node_modules folders, Python virtual environments and build outputs. Developer caches inside WSL can be just as large as on Windows: npm, pip, Cargo, Go and Gradle all keep their caches in your Linux home folder.

To find the biggest folders in your Linux home:

du -h --max-depth=2 ~ 2>/dev/null | sort -rh | head -20

Step 4: Shut down WSL

The virtual disks must not be in use while you compact them. Quit Docker Desktop from its icon in the notification area, then shut down WSL completely:

wsl --shutdown

Check that nothing is running with wsl --list --running. It should say there are no running distributions.

Step 5: Compact the virtual disk

Option A: Optimize-VHD (Windows Pro, Enterprise and Education)

Optimize-VHD is part of the Hyper-V PowerShell module. Run PowerShell as administrator:

Optimize-VHD -Path "$env:LOCALAPPDATA\Docker\wsl\disk\docker_data.vhdx" -Mode Full

Replace the path with the .vhdx you want to compact. If PowerShell says Optimize-VHD isn’t recognised, the Hyper-V management tools aren’t installed. Use option B instead.

Option B: diskpart (every edition, including Home)

  1. Open Terminal (Admin) and start diskpart:

    diskpart
  2. At the DISKPART> prompt, run these commands one at a time, with your own path:

    select vdisk file="C:\Users\YourName\AppData\Local\Docker\wsl\disk\docker_data.vhdx"
    attach vdisk readonly
    compact vdisk
    detach vdisk
    exit

Compacting can take several minutes for a large disk. Check the file size again afterwards. On a disk where you pruned a lot, it often drops by tens of gigabytes.

Before you compact Make sure you have a backup of anything important inside WSL, such as projects that aren’t pushed to a remote. Compaction is a routine operation, but a power cut in the middle of any disk operation can damage the file. wsl --export (below) is an easy backup.

If compaction fails

A few errors come up often. They almost always mean something still has the disk open.

  • “The process cannot access the file because it is being used by another process.” Docker Desktop or a WSL distribution is still running. Quit Docker Desktop from the notification area (not just its window), run wsl --shutdown again, and wait ten seconds. wsl --list --verbose should show every distribution as Stopped.
  • VS Code or another editor reconnects to WSL. Editors with a WSL connection can start the distribution again in the background. Close those windows before you compact.
  • “Optimize-VHD is not recognized.” The Hyper-V PowerShell tools aren’t installed, which is normal on Windows Home. Use diskpart.
  • diskpart says “Virtual Disk Service error: The file system is not compatible” or similar. Make sure you attached the disk as readonly and that the path points to the .vhdx file itself, in quotes.
  • The file barely shrinks. Either there was little free space inside the disk, or the space was freed in a way the compaction can’t see yet. Running sudo fstrim -av inside the distribution before wsl --shutdown tells the virtual disk which blocks are really free, and often makes the next compaction much more effective.

WSL 1 has no virtual disk

Distributions that still run as WSL 1 store their files directly in a folder on your Windows drive, not in a .vhdx. Check with wsl --list --verbose. For a WSL 1 distribution, deleting files inside Linux frees the space on Windows immediately, and there is nothing to compact.

Step 6 (optional): Move Docker or WSL off your C: drive

If your C: drive is small, the best long-term fix is to keep these disks on another drive.

Docker Desktop

Open Docker Desktop › Settings › Resources › Advanced, change Disk image location to a folder on another drive, and select Apply & restart. Docker moves its disk for you.

A WSL distribution

  1. Export the distribution to a file. This is also a full backup:

    wsl --export Ubuntu D:\Backups\ubuntu.tar
  2. Import it to a folder on the other drive, under a new name:

    wsl --import Ubuntu-D D:\WSL\Ubuntu D:\Backups\ubuntu.tar
  3. Start the new copy with wsl -d Ubuntu-D and check that your files are there.

  4. Only then remove the original with wsl --unregister Ubuntu.

Careful wsl --unregister permanently deletes the distribution and everything inside it. Run it only after you have confirmed the imported copy works.

After an import, WSL may start you as root. Set your normal user as the default in /etc/wsl.conf with a [user] section and default=yourname, then run wsl --terminate Ubuntu-D.

What about sparse VHDs?

Recent versions of WSL can mark a virtual disk as sparse, so it shrinks automatically as you delete files, with wsl --manage <distro> --set-sparse true. Some versions disable this by default because of data corruption reports, and a sparse disk can no longer be compacted with Optimize-VHD. Until it is fully supported in your version, regular manual compaction is the safer choice.

Limit how big Docker’s disk can get

Docker Desktop lets you cap the size of its virtual disk, so it can never quietly take over your drive. Open Settings › Resources › Advanced and lower Virtual disk limit to a size that fits your drive, for example 64 GB. When Docker reaches the limit, builds and pulls fail with “no space left on device”, which is your cue to prune.

WSL has a similar setting. In %USERPROFILE%\.wslconfig, a defaultVhdSize value under [wsl2] sets the maximum size for new distributions. Existing distributions keep their current maximum.

Keep it from growing back

  • Run docker system prune and docker builder prune every week or two if you build images often.
  • Use multi-stage builds and a .dockerignore file, so build contexts and images stay small.
  • Remove old Linux projects and their dependency folders when you finish with them.
  • Compact the virtual disks once a month, or after any large cleanup.
  • Put Docker’s disk image and big WSL distributions on a second drive.

How much will you get back?

It depends entirely on how you use Docker and WSL. On a developer machine that has built images for a year, pruning the build cache and unused images often frees 10–50 GB inside Docker, and compacting returns most of that to Windows. A WSL distribution used for a few projects typically compacts by a few gigabytes. Run docker system df before and after, and compare the .vhdx sizes, to see your own numbers.

Do it from one screen with FreeMyDisk

FreeMyDisk is a free, open-source disk cleaner for Windows 10 and 11, built for developers as much as for everyone else. Its Windows Optimizer › Virtualization section brings this guide together:

  • It lists your WSL distributions with the size of each virtual disk, and compacts them for you: it shuts the distribution down and runs the compaction, then reports how much was reclaimed.
  • It shows Docker’s reclaimable space and runs a safe prune (stopped containers, unused networks, dangling images and build cache, never volumes).
  • It finds Hyper-V virtual machine disks so you can see how much space your VMs take.

Next to that, its cleanup rules cover Docker Desktop’s logs and crash dumps, Podman and Rancher Desktop caches, and developer caches such as npm, pnpm, Yarn, pip, uv, Cargo, Go, Gradle, Maven and NuGet. Every item shows its size and a risk label, removals go to the Recycle Bin by default, and there is no telemetry.

Summary

  • Docker and WSL store data in .vhdx files that grow but don’t shrink on their own.
  • Prune first: docker system prune, docker builder prune, and clean up inside Linux.
  • Shut down WSL with wsl --shutdown.
  • Compact with Optimize-VHD (Pro) or diskpart (any edition).
  • Move Docker’s disk image and big distributions to another drive for a lasting fix. Never prune volumes or unregister a distribution without a backup.

Frequently asked questions

Why doesn't my Docker or WSL disk shrink after I delete files?

Docker Desktop and WSL 2 store their data in a virtual disk file (.vhdx) on Windows. The file grows when you add data, but deleting data inside Linux only frees space inside the virtual disk. You have to compact the .vhdx with Optimize-VHD or diskpart to give that space back to Windows.

Where is Docker Desktop's virtual disk on Windows?

On current versions it is %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx. Older versions used %LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx. You can see and change the location in Docker Desktop under Settings › Resources › Advanced.

Is docker system prune safe?

Yes. It removes stopped containers, unused networks, dangling images and the build cache. Running containers, tagged images and volumes are kept. Adding -a also removes every image not used by a container, and --volumes removes unused volumes, which can contain databases.

How do I compact a WSL disk on Windows Home?

Use diskpart, which is included in every edition. Run wsl --shutdown, open diskpart as administrator, then run select vdisk file="path\to\ext4.vhdx", attach vdisk readonly, compact vdisk, detach vdisk and exit.

Can I move Docker or WSL to another drive?

Yes. In Docker Desktop, change Disk image location under Settings › Resources › Advanced. For a WSL distribution, export it with wsl --export, import it on the other drive with wsl --import, and only then unregister the original.