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 File Server With POSIX ACLs in a Docker Container

Samba File Server With POSIX ACLs in a Docker Container
This article explains how to set up a Samba file server as an Active Directory domain member in a Docker container. This newer configuration differs from my earlier setup in one essential point: it uses POSIX instead of Windows ACLs, simplifying the administration and making it possible to modify files via other protocols than SMB. This post is part of my series on home automation, networking & self-hosting that shows how to install, configure, and run a home server with dockerized or virtualized services.
Home Automation, Networking & Self-Hosting

Latest Posts

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee
Filebrowser is being archived. As the author writes, the codebase dates back to when he was but 15 years old. Having created such a popular file management tool is a more than impressive achievement for anyone, let alone a teenager. However, when it comes to security and reliability, Filebrowser seems to be built on a less than ideal foundation, so much so that the author states:
Home Automation, Networking & Self-Hosting