Cover image for WSL Space Compression

WSL Space Compression

Words 228
Views
Visitors

Timeline

Timeline

2025-12-29

init

This article introduces specific methods for compressing the virtual disk space of WSL distributions, detailing the steps to find the distribution's disk file path and use the diskpart tool to perform disk compression operations after closing WSL.

.vhdxDisk path of the file

Replace distribution-name with the one seen viawsl -l -vthe name of the distribution seen

1
2
3
4
5
6
wsl -l -v

(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath") + "\ext4.vhdx"

# Take Arch Linux as an example
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'archlinux' }).GetValue("BasePath") + "\ext4.vhdx"

Compress the disk

Close WSL before compressing

1
wsl --shutdown

Then enter diskpart

1
diskpart

A new command line window will pop up. Enter the following content in sequence, replacing the value of file with the path obtained in the previous step

1
2
3
4
5
6
7
8
9
10
# Select the target disk file
select vdisk file="/path/to/your/.vhdx"
# Attach the virtual disk file in read-only mode
attach vdisk readonly
# Compact the virtual disk file
compact vdisk
# Detach the virtual disk file
detach vdisk
# Exit
exit

Reference