Reduce your WSL2 storage usage

Reduce your WSL2 storage usage

We've prepared some guidelines on how to reduce disk usage in your Windows Subsystem for Linux (WSL 2) installation size - as it sure can grow!

  1. Clean up Unused Software and Dependencies:
    • Over time, you may have installed packages that are no longer needed. Cleaning up these packages and their dependencies can free up disk space.
    • The autoremove command removes packages that were installed by other packages and are no longer needed. The clean command clears out the local repository of retrieved package files.
  2. Clean up Old Kernel Versions:
    • As new kernel versions are installed, old ones may accumulate, taking up disk space.
    • The --purge flag will remove the packages and their configuration files.
  3. Limit the Disk Space Used by WSL 2:
    • WSL 2 creates a Virtual Hard Disk (VHDX) to store the Linux filesystem. You can limit the disk space used by modifying the .wslconfig file.
    • This configuration limits WSL 2 to use 4GB of RAM and 2 processors.
  4. Shrink the VHDX File:
    • Over time, the VHDX file may become fragmented. Compacting this file can recover unused space.
    • Ensure WSL 2 is shut down before running the optimize-vhd command.
  5. Manual Cleanup:
    • Manually go through your filesystem and delete any unused files or directories.
  6. Utilize Docker Volumes:
    • If you're using Docker within WSL 2, consider using Docker volumes instead of bind mounts. Volumes are stored outside the Linux filesystem, saving space.
  7. Move Data to Windows Filesystem:
    • Store large files and datasets on the Windows filesystem, which can be accessed from WSL 2 at /mnt/c.
  8. Optimize Storage Configuration:
    • If using Docker, utilize smaller base images and multi-stage builds to reduce the size of Docker images.

These steps should help you reduce disk usage in your WSL 2 installation. Ensure to backup any important data before proceeding with deletions or file modifications.