Skip to content

Instantly share code, notes, and snippets.

@SH20RAJ
Last active October 5, 2025 18:07
Show Gist options
  • Save SH20RAJ/5db2a9ed50625ec07158c635831ac772 to your computer and use it in GitHub Desktop.
Save SH20RAJ/5db2a9ed50625ec07158c635831ac772 to your computer and use it in GitHub Desktop.
A “Clean Everything” Script for Macbook

🧹 CleanMac.sh — Ultimate macOS Cleanup Script

Author: Shaswat Raj (SH20RAJ) Script URL: View on Gist Compatible with: macOS (Intel + M1/M2/M3) Purpose: Free up space, clean developer junk, and keep your Mac lightning fast ⚡


🚀 Features

✅ Deletes heavy development folders:

  • node_modules, .next, .nuxt, .expo, dist, build, .vercel, .parcel-cache, etc.

✅ Clears all major caches:

  • npm, yarn, pnpm, Homebrew, CocoaPods, Docker, Xcode, system & user caches

✅ Removes junk safely:

  • Logs, temporary files, Trash, Downloads, screenshots, and app caches (Chrome, VSCode, Discord, etc.)

✅ Optional cleanup for:

  • iOS DerivedData, Docker images, large cached files (>1GB)

✅ Displays disk usage before and after cleanup


⚙️ Setup Instructions

1. Download the Script

You can either clone or directly download the gist:

curl -o ~/Desktop/cleanmac.sh https://gist.githubusercontent.com/SH20RAJ/5db2a9ed50625ec07158c635831ac772/raw/cleanmac.sh

💡 Tip: You can replace ~/Desktop/ with any folder you prefer.


2. Make It Executable

Give the script permission to run:

chmod +x ~/Desktop/cleanmac.sh

3. Run the Script

Now execute the cleanup:

./Desktop/cleanmac.sh

🧠 You may be asked for your password (for system-level cleanups).


🧠 Optional — Run Without Terminal (Automator App)

If you want to run this by double-clicking:

  1. Open Automator → select Application

  2. Choose Run Shell Script

  3. Paste:

    bash ~/Desktop/cleanmac.sh
  4. Save as 🧹 Clean My Mac (Dev).app

  5. Now double-click anytime to clean your system instantly.


🧩 Customization

You can easily edit the script to:

  • Target only specific folders

    find ~/Projects -type d -name "node_modules" -prune -exec rm -rf {} +
  • Skip certain cleanups → just comment out lines using #

  • Add your own paths (e.g., Flutter cache, PyCache, or JetBrains IDEs)

Example:

# rm -rf ~/Library/Caches/com.jetbrains.idea

🧾 Safe Mode (Preview Before Deleting)

If you want to see what will be deleted first:

find ~ -type d \( -name "node_modules" -o -name ".next" \)

💾 Schedule Auto Cleanup (Optional)

You can run it automatically once a week using crontab:

crontab -e

Then add:

0 12 * * 0 bash ~/Desktop/cleanmac.sh >> ~/cleanmac.log 2>&1

Runs every Sunday at 12 PM and logs results to ~/cleanmac.log.


⚠️ Notes

  • It’s safe for developers using Node.js, React, Next.js, Flutter, iOS, or Docker.
  • It won’t touch system-critical files or personal data like documents/photos.
  • Requires sudo for system logs cleanup.
  • Recommended to run once every 1–2 weeks.

🧠 Common Issues

❌ “Operation not permitted”

Run with sudo or give Full Disk Access to Terminal: System Settings → Privacy & Security → Full Disk Access → + Terminal

❌ “find: command not found”

Very rare — ensure you’re using the default bash or zsh shell (macOS ships with both).


❤️ Contribute or Suggest Additions

If you want to:

  • Add new cleanup targets (e.g., flutter, .venv, .pytest_cache)
  • Make it smarter (auto-detect developer tools)
  • Add GUI prompts or disk usage visualization

👉 Comment directly on the Gist Discussion or open a GitHub issue once this moves to a repo!


📸 Example Output

🧩 Deleting node_modules, .next, dist...
📦 Cleaning npm, yarn, pnpm caches...
🍺 Cleaning Homebrew cache...
🧰 Removing Xcode DerivedData...
🧠 Cleaning system caches...
🚀 Cleanup completed successfully!
📊 Disk usage before cleanup: 240 GB used
📊 Disk usage after cleanup: 180 GB used
✨ Done! Your Mac is now lighter and faster.

🧠 License

MIT License © Shaswat Raj

#!/bin/bash
# ==========================================================
# 🧹 CleanMac.sh — The Ultimate macOS Cleanup Script
# Author: Shaswat Raj (https://shaswat.live)
# Description: Deep-clean your Mac to free space safely.
# ==========================================================
# ⚠️ SAFETY FIRST
# This script will delete unnecessary caches, logs, and dev files.
# Always review before running. Run with:
# chmod +x cleanmac.sh
# ./cleanmac.sh
#
# You can comment out any section you don’t want to run.
# Each section is clearly labeled.
echo "🧹 Starting system cleanup... Please wait."
# ==========================================================
# 1. PROJECT FILES (Node, Next.js, React, etc.)
# ==========================================================
echo "🧩 Deleting node_modules, .next, .nuxt, dist, build folders..."
find ~ -type d \( \
-name "node_modules" -o \
-name ".next" -o \
-name ".nuxt" -o \
-name ".vercel" -o \
-name ".expo" -o \
-name ".expo-shared" -o \
-name ".parcel-cache" -o \
-name "dist" -o \
-name "build" \
\) -prune -exec rm -rf {} + 2>/dev/null
# ==========================================================
# 2. PACKAGE MANAGER CACHES (npm, yarn, pnpm)
# ==========================================================
echo "📦 Cleaning npm, yarn, pnpm caches..."
npm cache clean --force 2>/dev/null
yarn cache clean 2>/dev/null
pnpm store prune 2>/dev/null
# ==========================================================
# 3. HOMEBREW CLEANUP
# ==========================================================
echo "🍺 Cleaning Homebrew cache..."
brew cleanup -s 2>/dev/null
rm -rf "$(brew --cache)" 2>/dev/null
# ==========================================================
# 4. XCODE BUILD FILES
# ==========================================================
echo "🧰 Removing Xcode DerivedData and Device Support..."
rm -rf ~/Library/Developer/Xcode/DerivedData 2>/dev/null
rm -rf ~/Library/Developer/Xcode/iOS\ DeviceSupport 2>/dev/null
# ==========================================================
# 5. COCOAPODS & OTHER IOS DEV FILES
# ==========================================================
echo "📱 Removing CocoaPods caches..."
rm -rf ~/.cocoapods 2>/dev/null
rm -rf ~/Library/Caches/CocoaPods 2>/dev/null
# ==========================================================
# 6. USER CACHE & LOG FILES
# ==========================================================
echo "🧠 Cleaning system and user caches..."
sudo rm -rf /private/var/log/* 2>/dev/null
rm -rf ~/Library/Caches/* 2>/dev/null
rm -rf ~/Library/Logs/* 2>/dev/null
# ==========================================================
# 7. APP CACHES (Chrome, VSCode, Discord, etc.)
# ==========================================================
echo "💻 Deleting app caches (Chrome, VSCode, Discord)..."
rm -rf ~/Library/Application\ Support/Google/Chrome/Default/Service\ Worker/CacheStorage/* 2>/dev/null
rm -rf ~/Library/Application\ Support/Code/Cache/* 2>/dev/null
rm -rf ~/Library/Application\ Support/discord/Cache/* 2>/dev/null
rm -rf ~/Library/Application\ Support/Slack/Cache/* 2>/dev/null
# ==========================================================
# 8. DOCKER & CONTAINERS
# ==========================================================
echo "🐳 Cleaning Docker unused containers and images..."
docker system prune -a -f 2>/dev/null
# ==========================================================
# 9. DOWNLOADS & TRASH
# ==========================================================
echo "🗑️ Emptying Trash and Downloads..."
rm -rf ~/.Trash/* 2>/dev/null
rm -rf ~/Downloads/* 2>/dev/null
# ==========================================================
# 10. REMOVE LARGE CACHED FILES
# ==========================================================
echo "🧱 Finding and deleting files > 1GB in caches..."
find ~/Library/Caches -type f -size +1G -exec rm -f {} \; 2>/dev/null
# ==========================================================
# 11. CLEAN TEMP FILES
# ==========================================================
echo "🧊 Removing /tmp and other temp files..."
sudo rm -rf /private/tmp/* 2>/dev/null
rm -rf ~/tmp/* 2>/dev/null
# ==========================================================
# 12. OPTIONAL: OLD SCREENSHOTS & SCREEN RECORDINGS
# (Comment this section if you want to keep them)
# ==========================================================
echo "📸 Deleting screenshots and recordings from Desktop..."
find ~/Desktop -type f \( -name "Screenshot*" -o -name "Screen Recording*" \) -delete 2>/dev/null
# ==========================================================
# 13. REPORT DISK USAGE BEFORE & AFTER
# ==========================================================
echo "📊 Disk usage before cleanup:"
df -h /
echo "🚀 Cleanup completed successfully!"
echo "📊 Disk usage after cleanup:"
df -h /
echo "✨ Done! Your Mac is now lighter and faster."
echo "Tip: You can schedule this with crontab or Automator."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment