How to Configure WSL 2 Disk Space Reclamation Automation in Windows 11 Using PowerShell

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:

  1. Open PowerShell as your current user.
  2. Check if the global .wslconfig file exists in your user profile:
    Test-Path "$env:USERPROFILE\.wslconfig"
  3. 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
  4. 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.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.