How to Configure WSL 2 Sparse VHD Disks in Windows 11 Using PowerShell

Windows Subsystem for Linux (WSL 2) stores Linux distribution file systems inside dynamically expanding Virtual Hard Disks (ext4.vhdx). In earlier versions, while the virtual disk grew as files were created inside Linux, deleting files inside the distribution failed to shrink the host .vhdx file, leading to runaway storage bloat. Windows 11 introduces native sparse VHD support, allowing the virtual disk to shrink automatically in the background as space is freed.

Prerequisites for Enabling Sparse VHD Disks

Ensure your Windows 11 deployment satisfies the following criteria:

  • Windows 11 version 22H2 or newer.
  • WSL version 2.0.0 or later (verify by executing wsl --version in PowerShell).
  • Administrative privileges in PowerShell to modify disk attributes.

Converting an Existing WSL VHD to Sparse Format

If you already have a WSL distribution that has expanded to dozens of gigabytes, convert its virtual disk to a sparse file:

  1. Open an elevated PowerShell window as Administrator.
  2. Terminate all active WSL instances to unlock the disk file:
    wsl --shutdown
  3. Locate your distribution’s virtual disk path. For Ubuntu installed from the Microsoft Store, this is typically found within:
    $env:LOCALAPPDATA\Packages\CanonicalGroupLimited...\LocalState\ext4.vhdx
  4. Enable the sparse disk attribute using the native wsl command:
    wsl --manage Ubuntu --set-sparse true
  5. Windows immediately sets the sparse flag. From this point forward, when unallocated blocks are trimmed inside Linux, the underlying NTFS file system reclaims host disk space without manual compact operations.

Enabling Automatic Sparse Mode in .wslconfig

To ensure all newly created distributions and future VHD allocations default to sparse mode automatically, configure your global WSL parameters:

# Define global WSL configuration path
$wslConfigPath = "$HOME\.wslconfig"

# Set sparse VHD configuration
$configContent = @"
[experimental]
sparseVhd=true
autoMemoryReclaim=gradual
"@

Set-Content -Path $wslConfigPath -Value $configContent -Encoding UTF8
Write-Host "Configured sparseVhd in $wslConfigPath" -ForegroundColor Green

This automated configuration guarantees continuous disk and memory reclamation across all WSL 2 distributions.

Get the best tech tips delivered straight to your inbox.

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