Skip to content

Instantly share code, notes, and snippets.

@InTEGr8or
Created February 24, 2026 05:54
Show Gist options
  • Select an option

  • Save InTEGr8or/c415fe59f3d48f4e15adadd2cfb30b81 to your computer and use it in GitHub Desktop.

Select an option

Save InTEGr8or/c415fe59f3d48f4e15adadd2cfb30b81 to your computer and use it in GitHub Desktop.
WSL2 Disk Cleanup & Bloat Scanner Script

WSL Disk Cleanup Utility

A simple Bash script to scan and identify disk bloat in WSL2 (Windows Subsystem for Linux) environments.

Features

  • Scans top 15 largest directories in $HOME.
  • Reports sizes of common tool caches (Docker, Mise, UV, Playwright, NPM, etc.).
  • Identifies potential junk files like Java heap dumps (.hprof) and large installer files (.run).
  • Provides one-touch cleanup commands.

Installation

# Download and make executable
curl -L https://gist.githubusercontent.com/InTEGr8or/YOUR_GIST_ID/raw/wsl-cleanup.sh -o wsl-cleanup.sh
chmod +x wsl-cleanup.sh

# Recommended: Add an alias to your .bashrc
alias wsl-cleanup='~/wsl-cleanup.sh'

Usage

Run the scan:

./wsl-cleanup.sh

Run basic auto-cleanup (Docker, Mise, UV, and .hprof files):

./wsl-cleanup.sh --auto

Reclaiming Space on Windows Host

After cleaning files inside WSL, you must compact the virtual disk to see the space return to your Windows drive. Run this from Windows PowerShell (Admin):

wsl --shutdown
optimize-vhd -Path "C:\Users\<YourUser>\AppData\Local\Packages\<DistroPackage>\LocalState\ext4.vhdx" -Mode Full
#!/bin/bash
# WSL Cleanup & Bloat Scanner
# ---------------------------
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}--- WSL Disk Usage Scan ---${NC}"
# 1. Scan for large directories (Top 15)
echo -e "
${GREEN}Top 15 Largest Directories in Home:${NC}"
du -hd 1 ~ 2>/dev/null | sort -hr | head -n 15
# 2. Scan for specific common "bloat" locations
echo -e "
${GREEN}Common Tool Caches:${NC}"
# Define the cache locations we want to check
declare -A cache_paths=(
["Docker"]="/var/lib/docker"
["Mise (Runtimes)"]="$HOME/.local/share/mise"
["UV Cache"]="$HOME/.cache/uv"
["Playwright Browsers"]="$HOME/.cache/ms-playwright"
["Yarn Cache"]="$HOME/.cache/yarn"
["Pip Cache"]="$HOME/.cache/pip"
["NPM Cache"]="$HOME/.npm"
)
for cache in "${!cache_paths[@]}"; do
size=$(sudo du -sh "${cache_paths[$cache]}" 2>/dev/null | cut -f1)
if [ -z "$size" ]; then size="0B"; fi
printf "%-25s : %s
" "$cache" "$size"
done
# 3. Scan for "Garbage" files
echo -e "
${GREEN}Potential Junk Files:${NC}"
find $HOME -maxdepth 4 -name "*.hprof" -ls 2>/dev/null
find $HOME -maxdepth 2 -name "*.run" -size +100M -ls 2>/dev/null
echo -e "
${YELLOW}--- Recommended Cleanup Commands ---${NC}"
echo "1) docker system prune -a --volumes"
echo "2) mise prune"
echo "3) uv cache clean"
echo "4) npx playwright install --clear"
echo "5) rm -rf ~/.cache/*"
echo "6) find ~ -name '*.hprof' -delete"
if [[ "$1" == "--auto" ]]; then
echo -e "
${RED}Running Auto-Cleanup...${NC}"
docker system prune -f
if command -v mise &> /dev/null; then mise prune -y; fi
if command -v uv &> /dev/null; then uv cache clean; fi
find $HOME -name "*.hprof" -delete
echo -e "${GREEN}Basic auto-cleanup complete.${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment