This repository contains two bash scripts to help clean up your macOS system:
clean-cache.sh- A simple script to clear various caches (system, npm, yarn, build tools, Xcode, Gradle, Homebrew) and run periodic maintenance.mac-cleanup.sh- An interactive script that allows you to clean up specific categories or run an all-in-one cleanup.
- macOS
- Bash shell
- Some operations may require sudo (for system logs and periodic maintenance)
This script performs a cleanup without user interaction. It will:
- Clear system and application caches (
~/Library/Caches) - Clear npm and yarn caches (if installed)
- Remove build tool caches (
.next,node_modules/.vite,.angular/cache,dist) - Clear Xcode DerivedData
- Clear Gradle cache
- Run Homebrew cleanup (if installed)
- Run macOS periodic maintenance (requires sudo)
Script content:
#!/bin/bash
# cleanup-webdev.sh
# Script untuk bersihin cache web development di macOS
echo "π§Ή Starting cleanup..."
# 1. Bersihkan cache sistem & aplikasi
echo "π Clearing ~/Library/Caches..."
rm -rf ~/Library/Caches/*
# 2. Bersihkan npm & yarn cache
if command -v npm >/dev/null 2>&1; then
echo "π Clearing npm cache..."
npm cache clean --force
fi
if command -v yarn >/dev/null 2>&1; then
echo "π Clearing yarn cache..."
yarn cache clean
fi
# 3. Bersihkan folder build tools
echo "π Removing build caches (.next, vite, angular)..."
rm -rf .next node_modules/.vite .angular/cache dist
# 4. Bersihkan Xcode DerivedData
echo "π Clearing Xcode DerivedData..."
rm -rf ~/Library/Developer/Xcode/DerivedData/*
# 5. Bersihkan Gradle cache (Android Studio)
echo "π Clearing Gradle cache..."
rm -rf ~/.gradle/caches/
# 6. Bersihkan Homebrew cache
if command -v brew >/dev/null 2>&1; then
echo "π Running brew cleanup..."
brew cleanup
fi
# 7. Jalankan periodic maintenance
echo "π Running macOS periodic maintenance..."
sudo periodic daily weekly monthly
echo "β
Cleanup finished!"To run:
chmod +x clean-cache.sh
./clean-cache.shThis script provides an interactive menu to clean up specific areas:
- System Cache (
~/Library/Caches) - Downloads Folder
- Trash
- Xcode DerivedData
- Android Studio / Gradle Cache
- Docker Images & Containers
- Homebrew Cache
- Log Files (user logs only)
- Node Modules (current project) A. All-in-One Cleanup
- Exit
Script content:
#!/bin/bash
# mac-cleanup.sh
# Housekeeping script untuk macOS dengan opsi per kategori
# by Harys & Copilot
# Fungsi untuk hitung size folder
check_size() {
local path=$1
if [ -d "$path" ]; then
du -sh "$path" 2>/dev/null
else
echo "No directory: $path"
fi
}
# Fungsi untuk hapus cache user apps (skip protected)
clean_user_cache() {
echo "π Cleaning ~/Library/Caches (skip protected)..."
for dir in ~/Library/Caches/*; do
case "$dir" in
*CloudKit*|*FamilyCircle*|*Safari*|*com.apple*)
echo "β© Skip protected: $dir"
;;
*)
echo "π Removing: $dir"
rm -rf "$dir"
;;
esac
done
}
# Menu opsi
while true; do
echo "=============================="
echo "π§Ή Mac Cleanup Options"
echo "=============================="
echo "1. System Cache (~Library/Caches)"
echo "2. Downloads Folder"
echo "3. Trash"
echo "4. Xcode DerivedData"
echo "5. Android Studio / Gradle Cache"
echo "6. Docker Images & Containers"
echo "7. Homebrew Cache"
echo "8. Log Files"
echo "9. Node Modules (current project)"
echo "A. All-in-One Cleanup"
echo "0. Exit"
echo "=============================="
read -p "Pilih kategori: " choice
case $choice in
1)
echo "π¦ Size System Cache:"
check_size ~/Library/Caches
read -p "Hapus cache user apps? (y/n): " ans
[ "$ans" = "y" ] && clean_user_cache
;;
2)
echo "π¦ Size Downloads:"
check_size ~/Downloads
read -p "Hapus Downloads? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/Downloads/*
;;
3)
echo "π¦ Size Trash:"
check_size ~/.Trash
read -p "Empty Trash? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/.Trash/*
;;
4)
echo "π¦ Size Xcode DerivedData:"
check_size ~/Library/Developer/Xcode/DerivedData
read -p "Hapus DerivedData? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/Library/Developer/Xcode/DerivedData/*
;;
5)
echo "π¦ Size Gradle Cache:"
check_size ~/.gradle/caches
read -p "Hapus Gradle cache? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/.gradle/caches/*
;;
6)
echo "π¦ Docker Disk Usage:"
docker system df
read -p "Prune Docker (hapus semua unused)? (y/n): " ans
[ "$ans" = "y" ] && docker system prune -a -f
;;
7)
echo "π¦ Homebrew Cache Size:"
brew cleanup -n
read -p "Cleanup Homebrew? (y/n): " ans
[ "$ans" = "y" ] && brew cleanup --prune=all
;;
8)
echo "π¦ Log Files Size:"
check_size /var/log
check_size ~/Library/Logs
read -p "Hapus log files (user logs only)? (y/n): " ans
if [ "$ans" = "y" ]; then
echo "π Removing ~/Library/Logs/* (user logs)..."
rm -rf ~/Library/Logs/*
echo "β οΈ System logs in /var/log require sudo. Jalankan manual jika perlu:"
echo " sudo rm -rf /var/log/*"
fi
;;
9)
echo "π¦ Node Modules Size (current project):"
if [ -d "./node_modules" ]; then
check_size ./node_modules
read -p "Hapus node_modules? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ./node_modules
else
echo "β© Tidak ada folder node_modules di project ini."
fi
;;
A|a)
echo "π All-in-One Cleanup running..."
clean_user_cache
rm -rf ~/Downloads/* ~/.Trash/* ~/Library/Developer/Xcode/DerivedData/* ~/.gradle/caches/*
docker system prune -a -f
brew cleanup --prune=all
rm -rf ~/Library/Logs/*
[ -d "./node_modules" ] && rm -rf ./node_modules
echo "β οΈ System logs in /var/log require sudo manual cleanup."
echo "β
All-in-One Cleanup finished!"
;;
0)
echo "β Exit"
break
;;
*)
echo "Invalid choice"
;;
esac
done
echo "β
Done!"To run:
chmod +x mac-cleanup.sh
./mac-cleanup.shThen follow the on-screen prompts.
- Always review what you are deleting, especially when running scripts with sudo or removing system files.
- The scripts are provided as-is and you use them at your own risk.
- For system logs in
/var/log, you need to run cleanup manually with sudo if required.
The push.sh script is provided to easily push updates to the GitHub repository. It will:
- Add the remote origin (if not already added)
- Set the main branch
- Add all changes
- Commit with message "Update"
- Push to origin main
Script content:
#!/bin/bash
git remote add origin https://github.com/harys-rifai/mac-cleanup.git 2>/dev/null || true
git branch -M main
git add .
git commit -m "Update" || echo "No changes to commit"
git push -u origin mainTo use it after making changes:
chmod +x push.sh
./push.shThis script performs additional MacBook maintenance tasks:
- Clear DNS cache
- Check for software updates
- Clear Adobe cache (if installed)
- Clear Microsoft cache (if installed)
- Check for large files (>1GB) in home directory
- Check disk usage
Script content:
#!/bin/bash
# maintenance.sh
# Additional MacBook maintenance tasks
# by Harys & Copilot
echo "π§ Starting additional MacBook maintenance..."
# 1. Clear DNS cache
echo "π Clearing DNS cache..."
if command -v dscacheutil >/dev/null 2>&1; then
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
echo "β
DNS cache cleared"
else
echo "β οΈ dscacheutil not found, skipping DNS cache clear"
fi
# 2. Check for software updates
echo "π Checking for software updates..."
if command -v softwareupdate >/dev/null 2>&1; then
softwareupdate --list
else
echo "β οΈ softwareupdate command not found"
fi
# 3. Clear Adobe cache (if Adobe apps installed)
echo "π Checking for Adobe cache..."
if [ -d "~/Library/Application Support/Adobe" ]; then
echo "π¦ Adobe Application Support size:"
du -sh ~/Library/Application Support/Adobe 2>/dev/null
read -p "Clear Adobe cache? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/Library/Application Support/Adobe/* && echo "β
Adobe cache cleared"
else
echo "β© No Adobe Application Support folder found"
fi
# 4. Clear Microsoft cache (if Microsoft apps installed)
echo "π Checking for Microsoft cache..."
if [ -d "~/Library/Application Support/Microsoft" ]; then
echo "π¦ Microsoft Application Support size:"
du -sh ~/Library/Application Support/Microsoft 2>/dev/null
read -p "Clear Microsoft cache? (y/n): " ans
[ "$ans" = "y" ] && rm -rf ~/Library/Application Support/Microsoft/* && echo "β
Microsoft cache cleared"
else
echo "β© No Microsoft Application Support folder found"
fi
# 5. Check for large files (>1GB) in home directory
echo "π Checking for large files (>1GB) in home directory..."
find ~ -type f -size +1G 2>/dev/null | head -10
# 6. Check disk usage
echo "πΎ Disk usage:"
df -h
echo "β
Additional maintenance finished!"To run:
chmod +x maintenance.sh
./maintenance.shThis script is specifically designed for Apple Silicon M4 series MacBooks (Air and Pro). It provides advanced hardware diagnostics, thermal monitoring, battery health metrics, SSD SMART status, Rosetta 2 process detection, and performance optimization.
Key Features:
- Processor & Core Layout: Displays M4 core structures (Performance vs. Efficiency cores).
- Unified Memory Analysis: Analyzes current active, inactive, wired, compressed memory, and SSD swap file usage.
- Battery & Power Health: Reports cycle count, condition, and maximum capacity percentage.
- Disk & SMART Health: Verifies the hardware protocol and built-in SSD S.M.A.R.T. status.
- Thermal Diagnostic: Checks thermal warning levels (particularly crucial for fanless MacBook Air M4 models).
- Rosetta 2 Process Finder: Identifies running x86_64/Intel apps that may be impacting performance and battery efficiency.
- GPU Metal Shader & Cache Cleanup: Clears macOS system caches, user app caches, and Metal shader caches (forcing recompiles for optimum M4 GPU performance).
- DNS Cache & Software Updates: Flushes mDNSResponder cache and checks for macOS/Homebrew updates.
To run:
chmod +x m4-maintenance.sh
./m4-maintenance.shFeel free to fork this repository and submit pull requests for improvements.
MIT

