Skip to content

Instantly share code, notes, and snippets.

@LPX55
Created February 10, 2026 06:32
Show Gist options
  • Select an option

  • Save LPX55/9fbf80cf37f1fd03c884286eda3e187f to your computer and use it in GitHub Desktop.

Select an option

Save LPX55/9fbf80cf37f1fd03c884286eda3e187f to your computer and use it in GitHub Desktop.
WSL2 Force Reclaim Powershell Script
# ==========================================
# SAFE WSL VHDX OPTIMIZATION (NO BACKUP MODE)
# ==========================================
$VHD = "C:\path\to\vhd\ext4.vhdx"
$Log = "$env:USERPROFILE\wsl_optimize_log.txt"
Write-Output "`n=== SAFE WSL OPTIMIZATION START ===" | Tee-Object -FilePath $Log -Append
Write-Output "VHDX: $VHD" | Tee-Object -FilePath $Log -Append
# 1. Check file exists
if (!(Test-Path $VHD)) {
Write-Output "ERROR: VHDX not found. Aborting." | Tee-Object -FilePath $Log -Append
exit
}
# 2. Force shutdown WSL
Write-Output "Shutting down WSL..." | Tee-Object -FilePath $Log -Append
wsl --shutdown
Start-Sleep -Seconds 3
# 3. Ensure no WSL processes remain
$procs = Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "vmmem|vmmemWSL|wsl" }
if ($procs) {
Write-Output "ERROR: WSL-related processes still running:" | Tee-Object -FilePath $Log -Append
$procs | Tee-Object -FilePath $Log -Append
Write-Output "Aborting." | Tee-Object -FilePath $Log -Append
exit
}
# 4. Check for open handles on the VHDX
Write-Output "Checking for open handles..." | Tee-Object -FilePath $Log -Append
$locked = (Get-Process -ErrorAction SilentlyContinue | ForEach-Object {
try { $_.Modules } catch {}
} | Where-Object { $_.FileName -eq $VHD })
if ($locked) {
Write-Output "ERROR: VHDX is still in use. Aborting." | Tee-Object -FilePath $Log -Append
exit
}
# 5. Final confirmation before go
Write-Output "`nWARNING: No backup will be created due to low disk space." | Tee-Object -FilePath $Log -Append
Write-Output "Proceeding is safe ONLY because the VHDX is fully offline." | Tee-Object -FilePath $Log -Append
$confirm = Read-Host "Type YES to continue"
if ($confirm -ne "YES") {
Write-Output "User cancelled. Exiting." | Tee-Object -FilePath $Log -Append
exit
}
# 6. Optimize
Write-Output "Running Optimize-VHD..." | Tee-Object -FilePath $Log -Append
Optimize-VHD -Path $VHD -Mode Full
Write-Output "Optimization complete." | Tee-Object -FilePath $Log -Append
Write-Output "=== DONE ===`n" | Tee-Object -FilePath $Log -Append

When WSL Becomes the Disk

1. Clean up space inside WSL first

Shrinking only works if the free space inside the Linux filesystem is actually free.

For good measure

sudo apt update
sudo apt autoremove -y
sudo apt clean

Zero out free space (important for shrinking)

The magic sauce that most people tend to forget, missing out on huge amounts of available space

sudo dd if=/dev/zero of=zero.fill bs=1M
sudo sync
sudo rm zero.fill

2. Shut down WSL completely

From PowerShell (not inside WSL):

wsl --shutdown

Make sure the runtime is ACTUALLY OFFLINE, or you risk irreversible, colossal damage in the form of gigabytes of blood sweat and tears disappearing into thin air.


3. Locate your WSL virtual disk

Each distro has a file like:

%LOCALAPPDATA%\Packages\<DistroName>\LocalState\ext4.vhdx

You don’t need to open it — just know where it is.


4. Compact the VHDX using PowerShell

This is the safe, Microsoft‑recommended method.

Open PowerShell as Administrator:

Optimize-VHD -Path "C:\Users\<you>\AppData\Local\Packages\<Distro>\LocalState\ext4.vhdx" -Mode Full

This will shrink the virtual disk to the smallest size possible based on the free space you created earlier.


🧪 Be Smart: Use the Script Above

  • It doesn't take much for the "my hard drive needs space and WSL is being a hog" problem to escalate into a "massive, holy shit, major data loss problem."
  • Patience, as I have so regretfullyy learned, when dealing with such virtual drivves capable of so much destruction if elbowed the wrong way.
  • Anyways, successfully reclaimed 60GB after nearly two years of fighting with WSL2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment