Arch WSL Disk Cleanup
Guide systematic disk space cleanup on Arch Linux WSL systems. Follow a layered approach: start with safe cache cleanup, then review installed packages, then scan for large files and directories.
Pre-flight Checks
- Confirm the environment is Arch Linux:
head -3 /etc/os-release - Check if running in WSL:
grep -qi microsoft /proc/version - Check current disk usage:
df -h / - Identify available package managers:
which pacman yay paru 2>/dev/null
Record baseline for the final report — save the output of df -h / and:
du -sh /var/cache/pacman/pkg/ ~/.cache/ 2>/dev/null
Phase 1: Safe Cleanup (zero risk, execute directly)
Only delete caches and orphaned packages — no installed software is affected.
1.1 Package manager caches
# pacman package cache (keep current installed versions)
sudo pacman -Sc --noconfirm
# AUR build cache — use whichever helper is installed
yay -Sc --noconfirm 2>/dev/null || paru -Sc --noconfirm 2>/dev/null
# For aggressive cleanup (clear ALL cache including current versions):
# sudo pacman -Scc --noconfirm
1.2 Orphaned dependencies
# List orphans (no package depends on them)
pacman -Qdtq
# Remove them (guard against empty list)
orphans=$(pacman -Qdtq)
[ -n "$orphans" ] && sudo pacman -Rns --noconfirm $orphans || echo "无孤儿包"
1.3 Language package manager caches
Run whichever tools the user has installed:
# Python - uv
uv cache clean
# Python - pip
pip cache purge
# Node.js - npm
npm cache clean --force
# Node.js - bun
bun pm cache rm 2>/dev/null || rm -rf ~/.bun/install/cache/
# Rust - cargo
cargo cache --cleanup
# Or directly: rm -rf ~/.cargo/registry/cache ~/.cargo/registry/index
1.4 System logs
# Compress journalctl to 50M
sudo journalctl --vacuum-size=50M
# Delete old rotated logs
sudo find /var/log -type f \( -name "*.gz" -o -name "*.[0-9]" -o -name "*.old" \) -delete
Phase 2: Package Review (requires user confirmation)
2.1 Package statistics
echo "总包数: $(pacman -Qq | wc -l)"
echo "手动安装: $(pacman -Qe | wc -l)"
echo "AUR/外来包: $(pacman -Qm | wc -l)"
# Total installed size (script path is relative to this skill directory)
uv run --script scripts/calc_package_size.py
2.2 List manually installed packages
LC_ALL=C pacman -Qei | awk '
/^Name/{name=$3}
/^Installed Size/{
val=$4; unit=$5
if (unit=="GiB") mib=val*1024
else if (unit=="KiB") mib=val/1024
else mib=val
printf "%.0f MiB | %s\n", mib, name
}
' | sort -t'|' -k1 -rn
Present results as a sorted table. Never auto-delete packages — always ask the user to confirm which ones to remove.
Phase 3: Full Disk Scan
3.1 Top-level directory scan
# Use -x to stay on the current filesystem (critical on WSL!)
du -hx --max-depth=1 / 2>/dev/null | sort -rh | head -20
Critical: Always use du -x — without it, WSL will traverse mounted Windows partitions (/mnt/c, etc.) and report inflated sizes.
3.2 User directory deep scan
# Largest directories under $HOME
du -hx --max-depth=2 "$HOME" 2>/dev/null | sort -rh | head -30
# Recurse into common subdirectories (skip if they don't exist)
for dir in "$HOME/.cache" "$HOME/code" "$HOME/github"; do
[ -d "$dir" ] && du -hx --max-depth=2 "$dir" 2>/dev/null | sort -rh | head -15
done
3.3 Large file search
# System-wide files larger than 50MB
find /usr /opt /var -type f -size +50M -exec du -sh {} + 2>/dev/null | sort -rh | head -20
Phase 4: Targeted Cleanup
Safe to delete directly
| Pattern | Examples |
|---|---|
~/.cache/<tool>/ |
Playwright, camoufox, huggingface, go-build, jedi, node-gyp, telemetry caches |
**/build/ |
Compilation output directories |
**/node_modules/ |
Reinstallable via package manager |
**/.venv/ |
Rebuildable with uv/venv |
Require user confirmation
- Manually installed packages from Phase 2
~/service/,~/software/, or similar user-stored directories- Project code directories
- AI tool data directories (e.g.,
.genericagent/)
Use commands for specific targets
# Go caches
go clean -cache -modcache
# Direct directory deletion
rm -rf <target-directory>
Phase 5: WSL VHDX Compaction (manual — guide the user)
After cleanup inside WSL, the virtual disk file (ext4.vhdx) on Windows does not shrink automatically. The user must perform these steps manually outside WSL.
Walk the user through:
TRIM the filesystem (run inside WSL):
sudo fstrim -v /Shut down WSL (run in Windows PowerShell):
wsl --shutdownCompact the VHDX — choose one method (run in Windows PowerShell as Administrator):
Option A: diskpart
diskpart
# In diskpart:
select vdisk file="C:\Users\<username>\AppData\Local\Packages\...\LocalState\ext4.vhdx"
compact vdisk
exit
Option B: Hyper-V PowerShell cmdlet
Optimize-VHD -Path "C:\Users\<username>\AppData\Local\Packages\...\LocalState\ext4.vhdx" -Mode Full
The exact VHDX path depends on the WSL distribution — help the user locate it.
Post-cleanup Verification
# Check disk usage again
df -h /
# Show remaining cache sizes
du -sh /var/cache/pacman/pkg/ ~/.cache/yay/ 2>/dev/null
Reporting Format
Report results to the user as a table (use the baseline recorded in Pre-flight):
| Item | Before | After | Freed |
|---|---|---|---|
| pacman cache | X GiB | Y GiB | Z GiB |
End with a total freed amount summary.
Safety Rules
- Never auto-delete packages — always require user confirmation
- Never touch
/mnt/— those are Windows partitions - Always use
du -x— prevents counting mounted filesystems - rm -rf path blacklist — never rm -rf system directories:
/,/home,/etc,/usr,/var,/boot,/opt,/srv,/root. Only delete their subdirectories (e.g.~/.cache/playwright). - Handle permissions — some operations need sudo, some (like bun cache) may need
rm -rfinstead of CLI commands