Free Up Disk Space in WSL: The Ultimate Guide to Shrinking Your WSL Disk Image

If your WSL (Windows Subsystem for Linux) keeps growing in size, it’s not your imagination. Over time, cache files, Docker layers, and temporary data pile up inside your Linux instance — and your .vhdx file silently balloons.

Here’s a quick, proven way to reclaim gigabytes of storage without reinstalling WSL.

⚠️ Disclaimer:
This scripts directly modifies your .vhdx file. Run it only if you understand what it does — and always back up your WSL data first. You execute this at your own risk.

Step 1: Clean Cache and Temporary Files in WSL

Start by cleaning unnecessary packages and cached archives. Inside your WSL terminal, run:

sudo apt clean
sudo apt autoclean
sudo apt autoremove --purge -y

Step 2: Clean Up Docker (If Installed)

Docker images and volumes stored inside WSL often consume massive space in /var/lib/docker.

Use these commands to safely clean everything unused:

docker system prune -af
docker volume prune -f

Running this periodically keeps your WSL instance lean and fast.

Step 3: Shrink the .vhdx File Using DiskPart

Even after cleaning, your WSL virtual disk won’t shrink automatically. To actually free the space on your Windows drive, you’ll need to compact the .vhdx file.

3.1 Shut down WSL

wsl --shutdown

3.2 Compact the VHDX via DiskPart

Open PowerShell as Administrator and enter:

diskpart

Then inside DiskPart:

select vdisk file="C:\Users\<YourUser>\AppData\Local\Packages\<YourDistro>\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exitCode language: HTML, XML (xml)

This process can shrink your .vhdx by 30–70%, depending on how much unused data you cleared.

Добавить комментарий Отменить ответ