WSL Disk Space Cleanup

The Windows Subsystem for Linux (WSL) stores its virtual hard disks in the user profile on drive C:. These VHDX files can grow up to 1 TB in size, making it necessary to clean up and reclaim space occasionally. This article shows how.

Reclaim Disk Space Within the WSL VM

Start by cleaning up inside the WSL virtual machine.

Look for Large Directories With Ncdu

Install ncdu (NCurses Disk Usage) and use it to inspect your home directory for large folders. If you use WSL for software development, as I do, there may be huge leftovers from earlier builds.

Docker Cleanup

Remove Docker images and other files that are not used anymore:

sudo docker system prune -a

Reduce the Size of the WSL VM

Once the cleanup inside the WSL VM is complete, you can shrink the VM itself. Run the following commands on the Windows host.

Shut down WSL:

wsl --shutdown

Compact WSL’s virtual hard disk (VHDX) file:

# Launch the disk partition tool
diskpart

# Select your virtual disk
# In the path below, replace YOUR_NAME and GUID with your actual values
select vdisk file="C:\Users\YOUR_NAME\AppData\Local\wsl\GUID\ext4.vhdx"

# Attach it as read-only to prevent errors during compaction
attach vdisk readonly

# Run the compaction
compact vdisk

# Detach the virtual disk
detach vdisk

# Exit diskpart
exit

Move the WSL VM from C: to Another Disk

To move the Ubuntu WSL VM from C: to D:, run the following commands:

# Create a directory for the WSL VM
md d:\WSL\Ubuntu

# Shut down WSL
wsl --shutdown

# Move the VM
wsl --manage Ubuntu --move "D:\WSL\Ubuntu"

Notes

Devcontainer Startup Issues

If your devcontainer does not start after the cleanup, remove it so it can be recreated:

docker rm -f CONTAINER_NAME

Windows Disk Usage Tool

Use WizTree to explore disk usage on Windows. It is free for personal use and works well.

Comments

Related Posts

Samba & SMB Web Access Through Filestash With Passthrough Auth

Samba & SMB Web Access Through Filestash With Passthrough Auth
This article explains how to set up Filestash in a Docker container as a web interface for browser-based access to a Samba or SMB file server. This newer configuration improves on my earlier Filebrowser setup in one essential point: the SMB storage is accessed as the authenticating user, i.e., the user authentication is passed through to the backend storage. This means that each user sees exactly the directories and files they have access to. When a new file is created through Filestash, it’s stored with the proper permissions as if it had been created it via SMB.
Home Automation, Networking & Self-Hosting

Latest Posts