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!
- 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. Theclean
command clears out the local repository of retrieved package files.
- 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.
- 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.
- WSL 2 creates a Virtual Hard Disk (VHDX) to store the Linux filesystem. You can limit the disk space used by modifying the
- 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.
- Manual Cleanup:
- Manually go through your filesystem and delete any unused files or directories.
- 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.
- Move Data to Windows Filesystem:
- Store large files and datasets on the Windows filesystem, which can be accessed from WSL 2 at
/mnt/c
.
- Store large files and datasets on the Windows filesystem, which can be accessed from WSL 2 at
- 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.