Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

how to lower my wsl virtual disk size?

  1. User

    how to lower my wsl virtual disk size?

  2. Assistant

    To lower your WSL (Windows Subsystem for Linux) virtual disk size, you generally need to compact or shrink the virtual disk file (ext4.vhdx) used by your WSL distribution. Unfortunately, WSL does not provide a direct built-in command to shrink the virtual disk, but you can follow these steps to safely reduce its size:


    Step-by-step guide to shrinking your WSL virtual disk:

    1. Clean up the filesystem inside WSL

    • Open your WSL distribution (e.g., Ubuntu).
    • Clean up unnecessary files to reduce disk usage:
    sudo apt-get clean
    sudo apt-get autoremove
    rm -rf ~/.cache/thumbnails/*
    
    • You can also check disk usage inside WSL:
    df -h
    

    Make sure unused files are removed to minimize the data footprint.

    2. Export your WSL distribution

    Use PowerShell (run as Administrator):

    wsl --export <DistroName> <BackupFileName>.tar
    

    Example:

    wsl --export Ubuntu ubuntu_backup.tar
    

    Replace Ubuntu with your distro name (wsl -l -v lists them).

    3. Unregister the distribution

    WARNING: This step will remove your WSL distro, ensure your backup is safe!

    wsl --unregister <DistroName>
    

    Example:

    wsl --unregister Ubuntu
    

    This deletes the current distribution and its virtual disk.

    4. Re-import the distribution with a new, smaller VHDX file

    You can import it back with:

    wsl --import <NewDistroName> <InstallLocation> <BackupFileName>.tar --version 2
    

    Example:

    wsl --import Ubuntu C:\WSL\Ubuntu ubuntu_backup.tar --version 2
    

    This creates a new virtual disk sized appropriately to the contents of the .tar file, usually much smaller than the original.


    Alternative: Compact the VHDX file manually

    If you want to just compact the existing ext4.vhdx file:

    1. Locate your ext4.vhdx file:

    Default path:

    %USERPROFILE%\AppData\Local\Packages\<DistroPackageName>\LocalState\ext4.vhdx
    
    1. Shutdown all WSL instances:
    wsl --shutdown
    
    1. Compact the VHDX:

    Use PowerShell’s Optimize-VHD cmdlet (available with Hyper-V module):

    Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode Full
    

    Note: Hyper-V feature must be enabled for this cmdlet to be available.

    If Optimize-VHD is not available or fails, you can try third-party tools or convert VHDX to VHD and back, but this is more involved.


    Summary

    • Export + unregister + import method is the safest and easiest way to reduce size.
    • Compacting the VHDX directly is possible but requires shutdown and Hyper-V tools.
    • Clean up inside Linux is crucial before exporting to get better reduction.

    If you want a script I can help with that, or assist with details for your specific WSL distro/version.