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

Docker (Compose) Cheat Sheet

Docker (Compose) Cheat Sheet
This is a collection of tips and tricks I picked up while learning and working with Docker and Docker Compose on my home server and web server. Container Configuration Environment Variables Where to Define Environment Variables Environment variables are a common way to configure containers. To keep things organized, don’t put them in your Compose file but into dedicated files with the extension env. env_file vs. .env .env file: this “special” file can be used to set environment variable for use in the Compose file. The variables specified in .env are not available in the container. env_file: this section in the Docker Compose file lets you specify files that contain environment variables for use in the container. The variables specified in this section are not available in the Compose file. Bind Mounts vs. Docker Volumes Bind mounts let you control the directory structure. This has the advantage that you know exactly what gets stored where in the host’s file system. It has the disadvantage that you need to create the directory structure before you can start a container. Docker volumes are managed by the Docker engine. They’re stored in /var/lib/docker, “far away” from the Compose file. Personally, I very much prefer bind mounts because of the control they offer. I use subdirectories relative to the Compose file, e.g., ./data:/data. Keeping the container configuration and the container data in one place facilitates backups. Networking Expose vs. Ports Expose serves as documentation which ports a container is accessible on. Note: container ports are always accessible from other containers on the same Docker network. Ports makes container ports accessible to the host. Most of my services are accessible through the Caddy reverse proxy only. Opening ports to the host is, therefore, only rarely necessary. Static IP Address on the Host Network Use the Macvlan Docker network to attach a container directly to the host’s local network. Assign a static IP address by specifying the ip_range parameter in the ipam section of the Docker Compose file. See this configuration for an example. Disable Macvlan Container/Host Isolation Containers on a Macvlan network are isolated from the host. While the container can contact other machines on the local network, communications with the host are blocked. To work around that, create a virtual link with a route that points to the container’s IP address (example). Time Zone Containers should know about your local time zone. To achieve that, make it a habit to pass in /etc/localtime as a read-only volume to every container:
Virtualization & Containers