Skip to content

Instantly share code, notes, and snippets.

@ilKhr
Created February 2, 2024 13:25
Show Gist options
  • Save ilKhr/ce0916ade7861e74c694f5a9b553554e to your computer and use it in GitHub Desktop.
Save ilKhr/ce0916ade7861e74c694f5a9b553554e to your computer and use it in GitHub Desktop.
Resize WSL2 disk

Resize Docker (Link)

Turns out there is a way to shrink the vhdx images properly in windows:

  1. Run in cmd: docker system prune (optional --all), to clean up disk space
  2. Right mouse click on docker desktop in system tray -> quit
  3. Run in cmd: wsl --shutdown
  4. Run Optimize-VHD -Path "$env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx" -Mode Full, this will resize the vhdx images Same way you can try to find vhdx images that utilize too much space and optimize them as well. You can use TreeSize(Bing it: Tree Size Windows) to find which image takes the most of the space and optimize them.

P.S. dont forget to run docker system prune

Resize WSL2 (Link)

When the command let optimize-vhd is not available in your system do the following:

Shutdown the wsl before managing its disk

wsl --shutdown

Save the following script as compact-disk.txt

select vdisk file="C:\Users\%username%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk

Open prompt as Administrator and run the saved script above

diskpart /s <SAVED_SCRIPT_FOLDER_PATH>\compact-disk.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment