how to lower my wsl virtual disk size?
- User
how to lower my wsl virtual disk size?
- 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 -hMake sure unused files are removed to minimize the data footprint.
2. Export your WSL distribution
Use PowerShell (run as Administrator):
wsl --export <DistroName> <BackupFileName>.tarExample:
wsl --export Ubuntu ubuntu_backup.tarReplace
Ubuntuwith your distro name (wsl -l -vlists them).3. Unregister the distribution
WARNING: This step will remove your WSL distro, ensure your backup is safe!
wsl --unregister <DistroName>Example:
wsl --unregister UbuntuThis 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 2Example:
wsl --import Ubuntu C:\WSL\Ubuntu ubuntu_backup.tar --version 2This creates a new virtual disk sized appropriately to the contents of the
.tarfile, usually much smaller than the original.
Alternative: Compact the VHDX file manually
If you want to just compact the existing
ext4.vhdxfile:- Locate your ext4.vhdx file:
Default path:
%USERPROFILE%\AppData\Local\Packages\<DistroPackageName>\LocalState\ext4.vhdx- Shutdown all WSL instances:
wsl --shutdown- Compact the VHDX:
Use PowerShell’s
Optimize-VHDcmdlet (available with Hyper-V module):Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode FullNote: Hyper-V feature must be enabled for this cmdlet to be available.
If
Optimize-VHDis 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.