跳到正文
DEV Community· Pavan Kumar Sadashiv·· 3 小時前AI 評分33

如何在不使用可疑清理應用程式的情況下,從 MacBook 回收 25+ GB「隱藏」開發垃圾檔案

How I Reclaimed 25+ GB of "Hidden" Developer Cruft on My MacBook (Without Sketchy Cleaner Apps)

AI 導讀

作者示範如何用標準 shell 指令檢查 MacBook 儲存空間,並清理快取、閒置的全域 NPM 套件、本機 LLM 模型檔案及廢棄測試專案。文中實測在 5 分鐘內回收約 18+ GB,空閒空間由 47 GB 增至 64+ GB,沒有影響仍在使用的 Git 儲存庫或個人檔案。

正文

Every developer with a 256GB or 512GB MacBook eventually encounters the dreaded macOS storage graph: "System Data" eating 100+ GB, disk space dropping below 10%, and warnings interrupting your builds.

Most articles tell you to download paid third-party cleaner apps or blindly delete your cache folder. But as developers, we can audit our system with standard shell commands, target the real hogs, and reclaim 20 to 30+ GB in under 5 minutes—while keeping all active Git work 100% safe.

Here is the exact step-by-step developer playbook.


Step 1: Discover Where the Bloat Actually Lives

Instead of guessing, use du (disk usage) to find which folders in your user directory are consuming space:

# Check top-level folder sizes in your Home directory
du -d 1 -h ~ 2>/dev/null | sort -hr | head -n 20

On a developer machine, the top culprits almost always fall into 5 buckets:

  1. ~/.npm & global node packages (Cached tarballs, npx runners)
  2. ~/.ollama or HuggingFace (Forgotten local LLM model weights)
  3. ~/Library/Caches (Homebrew bottles, pip wheels, browser caches)
  4. ~/.rustup / ~/.nvm (Old language toolchains & duplicate runtimes)
  5. Forgotten test clones & scratch folders (Old benchmark or test repositories)

Let's purge them systematically.


Step 2: Safe Cache Purge (~5 to 10 GB)

These caches store copies of packages already installed on your machine. Purging them is 100% safe—your package managers will simply re-fetch only what they need in the future.

1. Clear NPM & NPX Runner Cache

If you frequently use npx or npm install, NPM silently hoards gigabytes of binaries and temporary package archives.

# Purge the npm download cache
npm cache clean --force

# Remove leftover npx temporary runners (often 1-2 GB by itself)
rm -rf ~/.npm/_npx

2. Purge Homebrew Downloaded Bottles

Homebrew saves cached .tar.gz bottles of every formula you ever installed or upgraded:

# Remove all stale lockfiles and out-of-date downloads
brew cleanup --prune=all -s

3. Clear Python Pip Wheels Cache

Pip stores wheels for every library version you've ever compiled or downloaded:

python3 -m pip cache purge

Step 3: Hunt Down Bloated Global NPM Packages (~2 to 5 GB)

We often npm install -g <tool> to test a CLI utility once and forget about it. Over time, heavy packages pull massive dependency trees into your global node_modules.

Find your largest global packages:

# Check the size of each globally installed npm package
du -d 1 -h $(npm root -g) 2>/dev/null | sort -hr | head -n 10

(In our audit, a single forgotten global package was secretly consuming **2.5 GB).

Uninstall what you don't need:

npm uninstall -g <package-name>

Step 4: Delete Abandoned Local LLM Weights (~5 to 20 GB)

If you experiment with local AI models (Ollama, LM Studio, Ollama CLI), small test models like Llama 3, Qwen, or DeepSeek can easily consume 5 GB to 30 GB silently in background blobs.

For Ollama:

# List all locally installed models and their sizes
ollama list

# Delete models you are no longer actively testing
ollama rm <model-name>

For HuggingFace / Transformers Cache:

# Check how much space HuggingFace models are eating
du -sh ~/.cache/huggingface 2>/dev/null

# Clean up models (or use the huggingface-cli delete-cache tool)
rm -rf ~/.cache/huggingface/hub/*

Step 5: Xcode & iOS Simulator Ghost Files (Mac Devs Only)

If you do any iOS or macOS development, Xcode is notorious for storing dead simulator runtimes and gigabytes of build indexes:

# Delete build artifacts and derived data
rm -rf ~/Library/Developer/Xcode/DerivedData/*

# Delete simulator cache for runtimes you no longer support
xcrun simctl delete unavailable

Step 6: The Golden Rule: Handle Repositories Safely!

When scanning for big folders, you will often spot large project trees. NEVER blindly delete code directories until you check their Git status!

Check before touching:

cd path/to/suspicious-repo
git status -s
git branch -a

If it's an active repository or contains unpushed work, do not delete the folder. Instead, delete the generated artifacts:

# Reclaim 1-3 GB instantly without losing your code or commits:
rm -rf node_modules

Whenever you return to the project, just run npm install (or pnpm install) to restore them.


Step 7: Clean Up Scratch & Finished Test Clones

If you keep a ~/scratch or ~/tmp-projects folder for testing open-source libraries or one-off experiments, check their sizes:

du -d 1 -h ~/scratch 2>/dev/null | sort -hr

Delete the ones whose merge requests are already merged or whose experiments are finished:

rm -rf ~/scratch/old-experiment-repo

The "Monthly Maintenance" Quick Script

Want to run the safe cleanups in a single terminal command? Add this function to your ~/.zshrc:

clean_mac_dev() {
  echo "🧹 Purging NPM cache..."
  npm cache clean --force 2>/dev/null
  rm -rf ~/.npm/_npx

  echo "🍺 Cleaning Homebrew bottles..."
  brew cleanup --prune=all -s 2>/dev/null

  echo "🐍 Purging Pip cache..."
  python3 -m pip cache purge 2>/dev/null

  echo "✨ Done! Check your available space with: df -h /"
}

Now, just type clean_mac_dev once a month to keep your SSD lean and responsive.


Results

By running this targeted audit:

  • Starting Available Space: 47 GB
  • Space Reclaimed: ~18+ GB in under 5 minutes
  • Final Available Space: 64+ GB
  • Active Git repositories & personal files affected: Zero.

What’s the largest hidden disk-hog you’ve discovered on your Mac? Drop it in the comments below!

來源:DEV Community · dev.to