Reclaim space used by WSL

02 Apr, 2023
WSLDocker

Reclaim space used by WSL

It is known that WSL 2 has a caracteristic that it does not free the space even after recovering some space within WSL distribution. So this will consume space on the host machine that is not being used. So we need to solve this issue manually. This manual was created based on Ubuntu 20.04 and WSL 2.

Tips to free up space

Free up space used by docker

One of the main reason we use WSL is to use docker. Se we end up consuming many spaces in downloading images, volumes, containers, etc. For our tests.

We can check the space used by docker with the following command:

docker system df

# Only volumes
docker system df -v

Can do is to remove all we dot not need anymore. To do this we can use the following commands:

docker system prune -a --volumes

This will remove all unused containers, networks, and images. By default, the prune command does not remove volumes to prevent data loss. But we can use the --volumes flag to remove all unused volumes.

Or remove individually:

# Remove all unused containers
docker container prune

# Remove all unused networks
docker network prune

# Remove all unused images
docker image prune

# Remove all unused volumes
docker volume prune

Free up spaces used by Docker Logs

truncate -s 0 /var/lib/docker/containers/**/*-json.log

Analyse the space used by distribution

The following command will analyse the space used by the distribution and show the top 10 biggest folders:

du -h <Folder to analyse> | sort -rh | head -n 10

# Example for the root folder
du -h / | sort -rh | head -n 10

Normally the WSL distribution has the host machine's disk mounted on /mnt You can prevent the previous command to analyse the host machine's disk by using the following command:

du -h / --exclude /mnt | sort -rh | head -n 10

Compact the WSL distribution

First stop the WSL

wsl --shutdown

Then find the disk of the distribution

cd C:\Users\%PROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\

You will see a file called ext4.vhdx that is the disk of the distribution. You will be able to verify that even after freeing up space within the distribution, the disk is still large.

To compact the disk we can use the following command:

optimize-vhd -Path .\ext4.vhdx -Mode full

Sometimes the previous command may not be available. In this case we can use the following steps:

First a file script. I am using the vscode but you can use any text editor.

code ./disk-compression.script

Paste the following script and save it:

select vdisk file="C:\Users\%username%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk

Then run the script as administrator:

diskpart /s ./disk-compression.script

IMPORTANT: Make sure the file ext4.vhdx is not used during this process so as not to corrupt the disk.

We are using cookies to ensure that we give you the best experience on our website. By clicking "Accept", you consent to the use of ALL the cookies. However you may visit Cookie policy to provide a controlled consent.