Last active
December 19, 2025 13:11
-
-
Save tazorax/f2c9d06f8bd814c5d31d203f7d18b323 to your computer and use it in GitHub Desktop.
Function to clean caches and heavy directories on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # clean-my-mac.zsh | |
| # | |
| # Usage: | |
| # 1️⃣ Save this file as ~/.zsh/clean-my-mac.zsh | |
| # 2️⃣ Edit WORK_DIR following you local setup | |
| # 3️⃣ Add the following line to your ~/.zshrc: | |
| # [ -f ~/.zsh/clean-my-mac.zsh ] && source ~/.zsh/clean-my-mac.zsh | |
| # 4️⃣ Reload your shell: | |
| # source ~/.zshrc | |
| # 5️⃣ Run: | |
| # clean-my-mac | |
| WORK_DIR=~/work | |
| clean-my-mac() { | |
| GOCACHE=${GOCACHE:-$(go env GOCACHE 2>/dev/null || echo "")} | |
| GOPKGMOD=${GOPATH:-~/go}/pkg/mod | |
| NPX_CACHE=${NPX_CACHE:-"$HOME/.npm/_npx"} | |
| COMPOSER_CACHE=${COMPOSER_CACHE:-"${COMPOSER_HOME:-$HOME/.composer}/cache"} | |
| NPM_CACHE=$(command -v npm >/dev/null 2>&1 && npm config get cache || echo "") | |
| PNPM_CACHE=$(command -v pnpm >/dev/null 2>&1 && pnpm store path || echo "") | |
| YARN_CACHE=$(command -v yarn >/dev/null 2>&1 && yarn cache dir || echo "") | |
| GREEN="\033[0;32m" | |
| YELLOW="\033[1;33m" | |
| NC="\033[0m" | |
| human_readable() { | |
| local SIZE=$1 | |
| local UNITS=("K" "M" "G" "T") | |
| local i=0 | |
| while (( SIZE > 1024 && i < ${#UNITS[@]}-1 )); do | |
| SIZE=$(( SIZE / 1024 )) | |
| ((i++)) | |
| done | |
| echo "${SIZE}${UNITS[i]}" | |
| } | |
| du_safe() { | |
| [[ -n "$1" && -d "$1" ]] && du -sk "$1" 2>/dev/null | awk '{print $1}' || echo 0 | |
| } | |
| echo -e "${YELLOW}🧮 Calculating estimated total size before cleaning...${NC}" | |
| TOTAL_BEFORE=$(( | |
| $(du_safe "$WORK_DIR") + | |
| $(du_safe "$NPM_CACHE") + | |
| $(du_safe "$PNPM_CACHE") + | |
| $(du_safe "$YARN_CACHE") + | |
| $(du_safe "$GOCACHE") + | |
| $(du_safe "$GOPKGMOD") + | |
| $(du_safe "$NPX_CACHE") + | |
| $(du_safe "$COMPOSER_CACHE") | |
| )) | |
| DOCKER_RECLAIM_KB=0 | |
| if command -v docker >/dev/null 2>&1; then | |
| if ! docker info >/dev/null 2>&1; then | |
| echo -e "${YELLOW}⚠️ Docker installed but not running.${NC}" | |
| echo -n "Do you want to start Docker now to include it? (y/N) " | |
| read answer | |
| if [[ "$answer" =~ ^[Yy]$ ]]; then | |
| open -a Docker | |
| echo -e "${YELLOW}⏳ Waiting for Docker to start...${NC}" | |
| while ! docker info >/dev/null 2>&1; do | |
| sleep 2 | |
| done | |
| fi | |
| else | |
| echo -e "${YELLOW}🐳 Docker is running.${NC}" | |
| fi | |
| fi | |
| # --- Clean caches --- | |
| echo -e "${YELLOW}🧹 Cleaning caches...${NC}" | |
| command -v npm >/dev/null 2>&1 && npm cache clean --force | |
| command -v pnpm >/dev/null 2>&1 && pnpm store prune | |
| command -v yarn >/dev/null 2>&1 && yarn cache clean | |
| [[ -d "$NPX_CACHE" ]] && rm -rf "$NPX_CACHE" | |
| [[ -d "$COMPOSER_CACHE" ]] && rm -rf "$COMPOSER_CACHE" | |
| # --- Remove heavy folders --- | |
| echo -e "${YELLOW}🗑️ Removing heavy folders in $WORK_DIR...${NC}" | |
| [[ -d "$WORK_DIR" ]] && find "$WORK_DIR" -type d \( -name "node_modules" -o -name ".serverless" -o -name ".terraform" -o -name "vendor" \) -prune -exec rm -rf {} + | |
| # --- Clean Docker --- | |
| if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then | |
| echo -e "${YELLOW}🐳 Cleaning Docker system...${NC}" | |
| DOCKER_OUTPUT=$(docker system prune --all --force --volumes 2>/dev/null) | |
| DOCKER_RECLAIM_STR=$(echo "$DOCKER_OUTPUT" | grep -i 'Total reclaimed space' | grep -Eo '[0-9.]+[KMGTP]?B?' | head -1) | |
| if [[ -n "$DOCKER_RECLAIM_STR" ]]; then | |
| NUMBER=$(echo "$DOCKER_RECLAIM_STR" | grep -Eo '[0-9.]+') | |
| UNIT=$(echo "$DOCKER_RECLAIM_STR" | grep -Eo '[KMGTP]B?') | |
| case $UNIT in | |
| B) DOCKER_RECLAIM_KB=$(echo "scale=0; $NUMBER / 1024" | bc) ;; | |
| KB|K) DOCKER_RECLAIM_KB=$NUMBER ;; | |
| MB|M) DOCKER_RECLAIM_KB=$(echo "$NUMBER * 1024" | bc) ;; | |
| GB|G) DOCKER_RECLAIM_KB=$(echo "$NUMBER * 1024 * 1024" | bc) ;; | |
| *) DOCKER_RECLAIM_KB=0 ;; | |
| esac | |
| fi | |
| fi | |
| # --- Clean Go caches --- | |
| command -v go >/dev/null 2>&1 && go clean -modcache && go clean -cache | |
| # --- Calculate sizes after --- | |
| TOTAL_AFTER=$(( | |
| $(du_safe "$WORK_DIR") + | |
| $(du_safe "$NPM_CACHE") + | |
| $(du_safe "$PNPM_CACHE") + | |
| $(du_safe "$YARN_CACHE") + | |
| $(du_safe "$GOCACHE") + | |
| $(du_safe "$GOPKGMOD") + | |
| $(du_safe "$NPX_CACHE") | |
| )) | |
| # --- Calculate total liberated space using bc for floating point --- | |
| TOTAL_LIBERATED_KB=$(echo "$TOTAL_BEFORE - $TOTAL_AFTER + $DOCKER_RECLAIM_KB" | bc) | |
| TOTAL_LIBERATED_MB=$(echo "scale=1; $TOTAL_LIBERATED_KB/1024" | bc) | |
| echo -e "${GREEN}✅ Cleaning completed!${NC}" | |
| echo -e "${GREEN}💰 Estimated total disk space freed: ${TOTAL_LIBERATED_MB}M${NC}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment