How to Clear Docker Desktop and WSL Space on Windows Using PowerShell

Here is a comprehensive beginner-friendly SEO article based on this chat, anonymised and ready to adapt:

Docker Desktop and WSL can consume a large amount of disk space over time, especially when working with containers, images, build cache, volumes, and Linux distributions. If your Windows machine is running low on storage, one of the first places to check is Docker Desktop and WSL.

This guide explains, in simple terms, how to remove unused Docker data, clean WSL files, fix common Docker connection errors, and compact the WSL virtual disk to actually recover space on Windows.

What Uses Space in Docker Desktop?

Docker Desktop can store several types of data locally:

  • Docker images
  • Stopped containers
  • Unused networks
  • Build cache
  • Docker volumes
  • WSL virtual disk files
  • Linux distribution data

Even after deleting containers or images, Windows may not immediately show the freed space. This is because Docker Desktop with WSL2 stores data inside a virtual disk file, usually an ext4.vhdx file. That virtual disk can grow over time and may need to be compacted manually.

Step 1: Check Docker Disk Usage

Open PowerShell and run:

docker system df

This command shows how much space Docker is using for:

Images
Containers
Local Volumes
Build Cache

If Docker is working correctly, you should see a summary of disk usage.

Step 2: Remove Unused Docker Data

To remove unused containers, networks, images, and build cache, run:

docker system prune -a

This removes:

Stopped containers
Unused networks
Unused images
Build cache

Docker will ask for confirmation. Type:

y

and press Enter.

Step 3: Remove Docker Volumes

If you also want to remove unused Docker volumes, run:

docker system prune -a --volumes

Be careful with this command.

Docker volumes can contain important data, such as:

MySQL databases
PostgreSQL databases
Redis data
Uploaded files
Application storage

Only use --volumes if you are sure you do not need the data stored in unused volumes.

Safer Docker Cleanup Commands

Instead of cleaning everything at once, you can clean Docker step by step.

Remove stopped containers:

docker container prune

Remove unused images:

docker image prune -a

Remove unused networks:

docker network prune

Remove build cache:

docker builder prune -a

Then check Docker disk usage again:

docker system df

Common Error: Docker Cannot Connect to Docker Desktop Linux Engine

Sometimes you may get an error like this:

error during connect: Get "http://%2F%2F.%2Fpipe%2FdockerDesktopLinuxEngine/v1.45/system/df":
open //./pipe/dockerDesktopLinuxEngine: The system cannot find the file specified.

This usually means Docker Desktop is not running, is stuck, or the Linux engine has not started.

Fix 1: Restart WSL and Docker Desktop

Run this in PowerShell:

wsl --shutdown

Then start Docker Desktop manually from the Start Menu.

Wait until Docker Desktop says the engine is running, then test again:

docker version
docker info
docker system df

Fix 2: Restart Docker Desktop from PowerShell

You can also restart Docker Desktop using PowerShell:

Stop-Process -Name "Docker Desktop" -Force -ErrorAction SilentlyContinue
Stop-Process -Name "com.docker.backend" -Force -ErrorAction SilentlyContinue
wsl --shutdown
Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe"

Wait for Docker Desktop to fully start, then run:

docker system df

Fix 3: Check WSL Distributions

Run:

wsl -l -v

You should normally see something like:

docker-desktop
docker-desktop-data
Ubuntu

If Docker Desktop distributions are missing or broken, open Docker Desktop and use the Troubleshoot or Reset options carefully.

Step 4: Clear Space Inside WSL

WSL itself can also use a lot of space. This is especially true if you use Ubuntu or another Linux distribution for development.

First, list your WSL distributions:

wsl -l -v

Example result:

Ubuntu                  Running
docker-desktop          Running
docker-desktop-data     Running

Open your Linux distribution:

wsl -d Ubuntu

Inside WSL, run:

sudo apt clean
sudo apt autoclean
sudo apt autoremove -y

These commands remove old package files and unused dependencies.

You can also remove temporary files:

sudo rm -rf /tmp/*

Remove user cache:

rm -rf ~/.cache/*

Check large folders in your home directory:

du -h --max-depth=1 ~ | sort -h

Check large folders in /var:

sudo du -h --max-depth=1 /var | sort -h

If you use Docker inside WSL, you can also run:

docker system df
docker system prune -a

Again, be careful with:

docker system prune -a --volumes

because it can delete unused database volumes.

Exit WSL:

exit

Step 5: Shut Down WSL

After cleaning Docker and WSL files, shut down WSL from PowerShell:

wsl --shutdown

This stops all running WSL distributions.

Step 6: Find the WSL Virtual Disk

WSL stores Linux data inside a virtual disk file named:

ext4.vhdx

For normal WSL distributions, the file is often stored under:

C:\Users\YourUser\AppData\Local\Packages

You can search for WSL virtual disks using PowerShell:

Get-ChildItem "$env:LOCALAPPDATA\Packages" -Recurse -Filter ext4.vhdx -ErrorAction SilentlyContinue |
Select-Object FullName, Length

For Docker Desktop, the virtual disk is usually here:

$env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx

Step 7: Compact the WSL Virtual Disk

Cleaning files inside WSL does not always reduce the visible disk usage in Windows. To actually reclaim the space, you may need to compact the virtual disk.

Open PowerShell as Administrator and run:

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

This compacts the Docker Desktop WSL disk.

For an Ubuntu WSL distro, replace the path with the correct ext4.vhdx path found earlier:

Optimize-VHD -Path "C:\Users\YourUser\AppData\Local\Packages\CanonicalGroupLimited...\LocalState\ext4.vhdx" -Mode Full

What If Optimize-VHD Is Not Available?

If PowerShell says Optimize-VHD is not recognized, you can use diskpart.

Open PowerShell as Administrator and run:

diskpart

Then inside DiskPart:

select vdisk file="C:\full\path\to\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

Make sure the path points to the correct ext4.vhdx file.

Practical Command Sequence for Docker Desktop

Use this if your main problem is Docker Desktop space:

docker system df
docker system prune -a
wsl --shutdown
Optimize-VHD -Path "$env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx" -Mode Full
docker system df

If you also want to remove unused volumes:

docker system prune -a --volumes

Use this only if you are sure you do not need the unused Docker volumes.

Practical Command Sequence for Ubuntu WSL

Inside Ubuntu:

sudo apt clean
sudo apt autoclean
sudo apt autoremove -y
rm -rf ~/.cache/*
exit

Then in PowerShell as Administrator:

wsl --shutdown
Get-ChildItem "$env:LOCALAPPDATA\Packages" -Recurse -Filter ext4.vhdx -ErrorAction SilentlyContinue |
Select-Object FullName, Length

After finding the correct path:

Optimize-VHD -Path "C:\full\path\to\ext4.vhdx" -Mode Full

Difference Between Docker Cleanup and WSL Disk Compacting

Docker cleanup removes unused Docker objects.

WSL cleanup removes unnecessary Linux files.

Virtual disk compacting reduces the actual .vhdx file size on Windows.

This means that if you only run:

docker system prune -a

you may clean Docker internally, but Windows may still show the same disk usage until you compact the virtual disk.

Important Safety Notes

Before running aggressive cleanup commands, remember:

docker system prune -a

removes unused images, containers, networks, and cache.

docker system prune -a --volumes

can remove unused volumes that may contain database or application data.

rm -rf ~/.cache/*

removes user cache inside WSL.

sudo rm -rf /tmp/*

removes temporary Linux files.

Always make sure you do not delete data you still need.

Recommended Beginner-Friendly Cleanup

For most users, this is a good starting point:

docker system df
docker system prune -a
wsl --shutdown

Then, if you need to recover Windows disk space physically, compact the Docker WSL disk:

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

If you also use Ubuntu or another WSL distro, clean inside that distro and compact its own ext4.vhdx file.

Conclusion

Docker Desktop and WSL can take a lot of disk space on Windows because they store data in containers, images, cache, volumes, and virtual disk files.

To properly clear space, you should:

  1. Check Docker disk usage.
  2. Remove unused Docker data.
  3. Clean inside WSL distributions.
  4. Shut down WSL.
  5. Compact the WSL virtual disk.

For Docker Desktop users, the most useful commands are:

docker system prune -a
wsl --shutdown
Optimize-VHD -Path "$env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx" -Mode Full

This combination removes unused Docker data and helps Windows reclaim the storage space.

SEO metadata

Meta title: How to Clear Docker Desktop and WSL Space on Windows

Meta description: Learn how to clear Docker Desktop and WSL space on Windows using PowerShell. Remove unused Docker images, containers, cache, volumes, and compact WSL virtual disks.

Suggested URL slug:
clear-docker-desktop-wsl-space-windows-powershell

Keywords/tags:
Docker Desktop cleanup, clear Docker space Windows, WSL cleanup, compact WSL disk, Docker system prune, PowerShell Docker cleanup, remove unused Docker images, clean Docker cache, WSL ext4.vhdx, Optimize-VHD Docker, Docker Desktop WSL2, free disk space Windows, Docker volumes cleanup, WSL disk space, Docker prune command

This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back