Skip to content

Instantly share code, notes, and snippets.

@mcdaqc
Last active February 12, 2025 01:08
Show Gist options
  • Save mcdaqc/563c7a92af8da9d911b4f358858d8504 to your computer and use it in GitHub Desktop.
Save mcdaqc/563c7a92af8da9d911b4f358858d8504 to your computer and use it in GitHub Desktop.
Shrink WSL2 and Docker Virtual Disks to Reclaim Disk Space

Shrink WSL2 and Docker Virtual Disks to Reclaim Disk Space

Over time, WSL2 distributions and Docker images can consume significant disk space. Even after deleting files, the allocated virtual disk (VHDX) does not automatically shrink. This guide explains how to manually reclaim disk space by optimizing the virtual disk.

Shrinking WSL2 Virtual Disks

Locate the VHDX File

On Windows 11, WSL2 distributions store their virtual disks in the following location:

C:\Users\username\AppData\Local\Packages\

Look for the vendor name corresponding to your installed distribution:

  • Ubuntu: CanonicalGroupLimited
  • Debian: TheDebianProject
  • Pengwin: WhitewaterFoundryLtd.Co

Once you identify the folder, navigate to the LocalState subdirectory, where you will find the .vhdx file for the distribution.

Optimize the Virtual Disk

  1. Shut down WSL2 completely:
    wsl --shutdown
  2. Open PowerShell as Administrator and run the following command:
    optimize-vhd -Path "C:\Path\To\Your.vhdx" -Mode full
    Replace C:\Path\To\Your.vhdx with the actual path to your VHDX file.

Shrinking Docker Virtual Disk

Locate Docker's Virtual Disks

Docker stores its virtual disk files for WSL2 in the following location:

C:\Users\YOURNAME\AppData\Local\Docker\wsl\disk
C:\Users\YOURNAME\AppData\Local\Docker\wsl\data

Optimize the Docker Virtual Disk

  1. Shut down Docker completely:
    wsl --shutdown
  2. Open PowerShell as Administrator and run:
    optimize-vhd -Path "C:\Users\YOURNAME\AppData\Local\Docker\wsl\disk\yourvdisk.vhdx" -Mode full
    Replace yourvdisk.vhdx with the actual VHDX file name if different.

You're done!

By following these steps, you can reclaim disk space occupied by WSL2 distributions and Docker images, improving overall storage efficiency on your system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment