Reclaim WSL disk space back #wsl #ubuntu #windows
WSL2 made many things right but there are still rough edges. For example, WSL sometimes doesn’t reclaim the relieved disk space back to windows immediately. It needs some encouragement. This is annoying while dealing with large files in WSL, the space is not occupied for so long.
Best way I’ve found so far is this:
Let’s see what’s occupying the most space
This can be done in any linux system. I use ubuntu in my WSL so feel free to use it to save up space in your linux drives too.
Windirstat: Check what’s happening in your drive
One of the coolest windows tools i’ve ever used: https://sourceforge.net/projects/windirstat/
- Check disk occupation size:
- Find Out Top 25 Directories and Files (Disk Space) in Linux
du -a --block-size=1G / | sort -n -r | head -n 25
- This one is a little more advanced which is doing a bigger scan but I also don’t want actual windows files to be scanned so excluded /c/ or /mnt/c and/or any other drives.
sudo du -ch / --exclude=/mnt/c -a --block-size=1M | sort -n -r | head -n 25
- Usually the culprit is /tmp file. Recommend deleting its contents every now and then.
- Free up some space in WSL and make sure it went down with
df -h. Otherwise compacting operation below won’t be much of a help.
Take the space back by compaction
Found this cool trick to compact the vhdx file which is the virtual disk for ubuntu WSL: https://github.com/microsoft/WSL/issues/4699
wsl --shutdown # Or this method: wsl -l # Lists all your distros in WSL # Shuts down the distro. Otherwise diskpart won't work since the drive is mounted. wsl -t your_distro_name diskpart # Open window Diskpart select vdisk file="C:\Users\$user\AppData\Local\Packages\CanonicalGroupLimited\…\ext4.vhdx" attach vdisk readonly compact vdisk detach vdisk exit
Enjoy your free space