skip to content
clipboard

How to Shrink WSL2 Virtual Disks and Docker Images

If you’re a developer using Windows Subsystem for Linux 2 (WSL2) and Docker, you might notice that your disk space gets eaten up over time. This is because WSL2 virtual disks and Docker images can grow but don’t automatically shrink when you delete files. Here’s how to reclaim that precious disk space.

Understanding the Problem

WSL2 uses a Virtual Hard Disk (VHD) to store your Linux files. Similarly, Docker stores images and containers in its own storage area. Both of these can grow over time, but they don’t automatically shrink when you delete files within them.

Shrinking WSL2 Virtual Disk

  1. First, shut down WSL:
wsl --shutdown
  1. Find your distribution’s virtual hard disk. It’s usually located at:
%USERPROFILE%\AppData\Local\Packages\<DistroPackageName>\LocalState\ext4.vhdx
  1. Optimize the disk within WSL:
sudo fstrim -av
  1. Use the Optimize-VHD command in PowerShell (run as Administrator):
Optimize-VHD -Path "path-to-your-ext4.vhdx" -Mode Full

Cleaning Up Docker

  1. Remove unused containers:
docker container prune
  1. Remove unused images:
docker image prune -a
  1. Remove unused volumes:
docker volume prune
  1. Remove all unused Docker resources:
docker system prune -a

Best Practices to Prevent Disk Space Issues

  1. Regularly clean up unused Docker resources
  2. Use .dockerignore files to exclude unnecessary files
  3. Implement multi-stage builds to reduce final image size
  4. Periodically run disk optimization commands
  5. Monitor disk usage with tools like Windows Storage Sense

Additional Tips

  • Use docker system df to check Docker disk usage
  • Consider using WSL2’s automatic memory reclamation feature
  • Keep your WSL2 and Docker installations updated
  • Use temporary directories for build artifacts

By following these steps and best practices, you can maintain better control over your disk space while using WSL2 and Docker. Remember to perform these maintenance tasks regularly as part of your development workflow.