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

Compacting Client Hyper-V VHDX Files

Compacting Client Hyper-V VHDX Files
Virtual hard disks have the same tendency to grow in size as regular disks have to fill up. Deduplication is a great way to battle this, but unfortunately it is not available for Windows 8 Client Hyper-V. I know that hacks are available describing how to transfer the relevant DLLs from Server 2012 but I value my data too much to try that. The only thing left in order to regain valuable (SSD) disk space is to compact the VHDX. That, however, is more difficult than it should be.
Virtualization & Containers

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. Please note that in my own use this configuration has been superseded by Sambee, my very own browser-based file viewer and manager. Sambee has a more polished and user-friendly UI, is optimized for desktop and mobile, displays Markdown not as code but rendered, has powerful search and navigation capabilities, keyboard shortcuts, and much more.
Home Automation, Networking & Self-Hosting

Latest Posts