By default, dynamic Virtual Hard Disks (ext4.vhdx) utilized by WSL 2 expand automatically when files are created inside Linux distributions, but they do not automatically shrink when files are deleted. Over time, build caches, temporary packages, and Docker volumes consume tens of gigabytes of unreleased host drive space. Modern Windows 11 updates introduce native sparse VHD and automatic disk reclamation modes that can be automated through global configuration files and PowerShell maintenance scripts.
Understanding WSL 2 Automatic Space Reclamation Features
Recent versions of WSL 2 running on Windows 11 support automated disk compaction via two complementary mechanisms:
- Sparse VHD Mode: Formats the virtual hard disk so that when Linux issues discard or TRIM requests (e.g., via
fstrim), the underlying host NTFS volume automatically frees physical disk sectors. - Automatic Disk Reclamation (
autoMemoryReclaim): Instructs the lightweight virtual machine utility to reclaim cached memory and compact unused disk pages when the machine enters idle states. - Scheduled Maintenance Scripts: Automates periodic discard runs across all configured distributions using Windows Task Scheduler and PowerShell.
Configuring .wslconfig for Automatic Disk Reclamation
Configure the global WSL engine configuration file to enable automatic memory and disk space recovery:
- Open PowerShell as your current user.
- Check if the global
.wslconfigfile exists in your user profile:Test-Path "$env:USERPROFILE\.wslconfig" - Append or create the configuration file with the following experimental reclamation directives:
$configContent = @" [wsl2] autoMemoryReclaim=gradual sparseVhd=true [experimental] autoMemoryReclaim=dropcache "@ $configContent | Out-File -FilePath "$env:USERPROFILE\.wslconfig" -Encoding utf8 - Restart the WSL subsystem to apply the global configuration:
wsl --shutdown
Converting Existing VHDX Files to Sparse Mode via PowerShell
If your distribution’s virtual disk was created before sparse VHD support was released, convert the existing VHDX container:
# Shutdown active instances
wsl --shutdown
# Locate the distribution VHDX file
$vhdx = (Get-ChildItem -Path "$env:LOCALAPPDATA\Packages" -Filter "ext4.vhdx" -Recurse).FullName
# Convert the container to a sparse disk using the wsl command
wsl --manage Ubuntu --set-sparse true
Once converted, host drive space is freed whenever files are removed inside the distribution.
Automating Periodic TRIM Runs via Scheduled PowerShell Script
To ensure that discarded file blocks are actively released without manual intervention, create an automated PowerShell task to issue TRIM commands to all active distributions:
# Define the automated compaction script
$scriptPath = "$env:USERPROFILE\Documents\WSL_Reclaim.ps1"
$scriptBody = @"
# Trigger internal filesystem TRIM across distributions
wsl -e sudo fstrim -av
Write-Output "WSL filesystem TRIM completed successfully."
"@
$scriptBody | Out-File -FilePath $scriptPath -Encoding utf8
# Register scheduled task to execute weekly
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle Hidden -File `"$scriptPath`""
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am
Register-ScheduledTask -TaskName "WSL_Auto_Reclaim" -Action $action -Trigger $trigger -Description "Periodic WSL disk space reclamation"
Verifying Reclaimed Storage Space
Verify that host storage capacity has been restored by comparing physical disk utilization against virtual size:
# Check physical size of the VHDX file on the host NTFS volume
Get-ChildItem -Path "$env:LOCALAPPDATA\Packages" -Filter "ext4.vhdx" -Recurse | Select-Object Name, @{Name="SizeGB"; Expression={[math]::round($_.Length / 1GB, 2)}}
This automated configuration guarantees that WSL 2 dynamically shrinks after substantial build processes, preserving available SSD storage.