Inspired by this post on GitHub about WSL2 filling hard disks. Original code by mikemaccana / compact-wsl2-disk.
This script will:
- Check for an elevated shell.
- Find your
vhdx
file(s) in the default paths of- WSL2 distros (
C:\Users\<NAME>\AppData\Local\Packages\CanonicalGroupLimited...
) - Docker WSL2 distros (
C:\Users\<NAME>\AppData\Local\Docker
)
- WSL2 distros (
- Shut down WSL2
- Compact all found disks with
diskpart
After that just open another Linux terminal and WSL2 will restart automatically.
-
I am a stranger on the internet asking you to run a powershell script. You should probably look inside the script
compact-wsl2-disk.ps1
and make sure you understand what it does. -
This will stop WSL. So save your work in Linux.
Open Powershell as Administrator and run:
powershell.exe .\compact-wsl2-disk.ps1
Please file pull requests instead of just asking for things. Thanks!
The following fixes were made to the original code:
- Script elevation.
- Use
Push-Location
andPop-Location
to get back to the same location you started at. - Support for account names with spaces like
Kees Bakker
. - Script lines are made yellow.
- PowerShell linted.
MIT
This person on StackOverflow and everyone on the WSL GitHub and R0Wi.