WSL – Reclaiming Storage Space

I’ve been a big fan of WSL ever since it came out – that to this solution, I can work daily with Windows, in my opinion the best and the most flexible OS, and still have all Linux advantages and a bit isolated work environment. Of course, WSL is not perfect and have some issues. One of them is taking up disk space. During our work, the size of the WSL file will become larger and larger. It will happen even if we will decide to jump into Linux distribution and remove a lot of unnecessary files. Fortunately, there is a quick solution – we can use diskpart includes in Windows to reclaim some space.

I’ve already written about one of possible solutions in the beginning of 2022 – that method is still valid, but a bit more complicated, because we need to enable Hyper-V components, restart computer, then reclaim space and… disable these components again, because in other case, our computer may not sleep correctly. Yes, it’s like a domino effect and everything is linked. Fortunately, it’s not only one method. The second one is much simpler and provides the same results. Let’s free some space.

Table of Contents

Preparations

Before we will reclaim space, it is very good option to remove as much data as possible from WSL distribution – unused and unnecessary files, temp data, docker leftovers etc. There I propose some solutions you can use:

Remove packages leftovers (Debian & Ubuntu)

This command will remove leftovers, unused dependencies etc.

You can use similar command also for different distros based on your package manager

sudo apt-get autoremove && sudo apt-get autoclean

Clean temp directory:

It will remove temporary files. This command may display warning about some files – it’s fine

sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*

Remove docker leftovers

Useful only if you use docker inside WSL – this command will remove all unused containers, images, volumes and networks. Depends on your work, may free up a lot of space

docker system prune

Remove other files

Rest depends on your usage – you should know best what files you are storing and where. If you create some web apps, you can remove some generated assets and caches from directories. It is worth to also check user directory and remove all unused data

Using diskpart

Ok, right now we are prepared to reclaim space used by WSL. Subsystem must be disabled, so run this command from Windows terminal level first:

wsl --shutdown

It will shut down all WSL distros. Now we can jump into diskpart, type in terminal:

diskpart

There will be privilege elevation prompt (UAC) – confirm that. Windows will automatically start build in diskpart command in separate Windows. It’s not as powerful as a standard terminal, but for our needs, it does not matter a lot.

First thing we need to do is to select the virtual disk file used by WSL:

select vdisk file="{PATH_TO_YOUR_WSL_VHDX_FILE}"

How can you find the proper file? It depends on distribution. In default, Windows stores all these files in the user AppData directory. For example, in Ubuntu scenario it is:

SYSTEM_DRIVE\Users\{YOUR_USERNAME}\Local\Packages\CanonicalGroupLimited(...)\LocalState\ext4.vhdx

You can use Windows search or alternative search engines (like Everywhere) to locate all “vhdx” files.

Then, we need to attach this virtual disk. We will use read only option because we do not want to modify any data, just reclaim space used by this image.

attach vdisk readonly

Finally, we can start compacting process:

compact vdisk

It will take some time and the system will inform us about progress.

After completion, we should detach virtual disk

detach vdisk

You need to repeat these steps for all vhdx files you want to compact.

And it’s all!

Your virtual disk file size should be smaller after this process. It may not reflect all changes you made inside WSL – for example, I had a WSL image with a size of ~120 GB. I’ve removed about 30 GB inside WSL and then compact, but after process, WSL image is still ~100 GB instead of 90 GB as you would expect. It’s completely fine, compacting cannot cover everything. If your base image was very small and compacting can not help anymore, the only option you have is to reset WSL: remove current distro and configure it again.

Summary

As you can see, diskpart option is maybe a bit more complex in terms of number of commands required to run, but of course it does not need any additional components. You do not need to restart the computer, so operation is smooth, and you can free up WSL space at any time.

I hope it will help you reclaim your space. If you have any feedback, please contact me

One thought on “WSL – Reclaiming Storage Space

Comments are closed.