The Problem
I recently encountered an unexpected disk space issue on my NixOS system. After running without a restart for 5 days, relying only on suspend/sleep and performing several system updates, I discovered my /nix/store
directory had bloated to 100GB.
Root Cause
The issue stemmed from:
- Not restarting the system for an extended period (affected some GC roots)
- Multiple system updates accumulating without a full restart
- Components like the window manager waiting for a restart to complete updates
- Old software versions and generations remaining in the store
Simple Solution
Two steps resolved the issue:
- Restart your system
- Run
sudo nix-collect-garbage --delete-older-than 7d
After these steps, my /nix/store
size decreased to 44GB.
Additional Cleanup Commands for Developers
Development Tools Cache Cleanup
# Package Manager Caches
pnpm store prune --force # PNPM global store (Node.js)
rm -rf .gradle/caches # Gradle (Java/Android)
cargo clean -Z gc # Rust (nightly only)
# Go Caches
go clean -modcache
go clean -cache
# Docker
docker system prune -a -f # Removes all unused containers, networks, images (both dangling and unreferenced)
Node.js Projects Cleanup
# Find node_modules directories (creates a review file)
find . -type d -name "node_modules" -not -path "*/node_modules/**/node_modules" | tee reviewed_node_modules.txt
# After reviewing, delete the selected directories
cat reviewed_node_modules.txt | xargs -I {} rm -rf {}
rm reviewed_node_modules.txt
System Analysis and Additional Cleanup
# Analyze disk usage
nix-shell -p ncdu --command 'ncdu /'
# Devenv.sh cleanup
devenv gc
# Application cache cleanup
rm -rf .cache/spotify/Data
Core Dumps Review (Optional)
# Locate core dumps (you need to manually delete them)
find / -type f -name 'core' 2>/dev/null
Warning: Exercise caution with these commands. They can cause data loss if used improperly. Always review what you’re deleting and ensure you have backups of important data.
Tips
- Regularly restart your system after updates
- Use
ncdu
to identify space-consuming directories - Review cleanup commands and their implications before execution
- Keep track of development tool caches, especially in large projects
I hope this helps others who might encounter similar issues!