#!/bin/bash # Clean up temporary and old files # Usage: clean.sh [--dry-run] set -euo pipefail CLAUDE_DIR="${HOME}/.claude" DRY_RUN="${1:-}" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' echo "๐Ÿงน Claude Code Cleanup" echo "" if [[ "${DRY_RUN}" == "--dry-run" ]]; then echo -e "${YELLOW}Dry run mode - no files will be deleted${NC}" echo "" fi total_freed=0 # Helper to delete files delete_files() { local pattern="$1" local description="$2" local max_age="${3:-}" local files if [[ -n "${max_age}" ]]; then files=$(find ${pattern} -type f -mtime +${max_age} 2>/dev/null || true) else files=$(find ${pattern} -type f 2>/dev/null || true) fi if [[ -n "${files}" ]]; then local count=$(echo "${files}" | wc -l) local size=$(echo "${files}" | xargs du -ch 2>/dev/null | tail -1 | cut -f1) echo "${description}: ${count} files (${size})" if [[ "${DRY_RUN}" != "--dry-run" ]]; then echo "${files}" | xargs rm -f echo -e " ${GREEN}โœ“${NC} Deleted" else echo " Would delete:" echo "${files}" | head -5 | sed 's/^/ /' [[ $(echo "${files}" | wc -l) -gt 5 ]] && echo " ..." fi else echo "${description}: nothing to clean" fi echo "" } # Old workflow logs (older than 30 days) echo "=== Workflow Logs ===" delete_files "${CLAUDE_DIR}/logs/workflows/*.log" "Old logs (>30 days)" 30 # Old health reports (older than 30 days) echo "=== Health Reports ===" delete_files "${CLAUDE_DIR}/logs/health-reports/*.md" "Old reports (>30 days)" 30 # Old backups (keep last 10) echo "=== Backups ===" BACKUP_DIR="${CLAUDE_DIR}/backups" if [[ -d "${BACKUP_DIR}" ]]; then backup_count=$(ls -1 "${BACKUP_DIR}"/*.tar.gz 2>/dev/null | wc -l || echo "0") if [[ ${backup_count} -gt 10 ]]; then old_backups=$(ls -1t "${BACKUP_DIR}"/*.tar.gz | tail -n +11) count=$(echo "${old_backups}" | wc -l) echo "Old backups (keeping last 10): ${count} files" if [[ "${DRY_RUN}" != "--dry-run" ]]; then echo "${old_backups}" | xargs rm -f echo -e " ${GREEN}โœ“${NC} Deleted" else echo " Would delete:" echo "${old_backups}" | head -3 | sed 's/^/ /' fi else echo "Backups: ${backup_count} files (keeping all)" fi else echo "Backups: no backup directory" fi echo "" # Project cache files echo "=== Project Cache ===" if [[ -d "${CLAUDE_DIR}/projects" ]]; then # Count files in projects dir project_files=$(find "${CLAUDE_DIR}/projects" -type f 2>/dev/null | wc -l) project_size=$(du -sh "${CLAUDE_DIR}/projects" 2>/dev/null | cut -f1 || echo "0") echo "Project cache: ${project_files} files (${project_size})" echo " (managed by Claude Code, not cleaning)" else echo "Project cache: not present" fi echo "" # Stats cache echo "=== Stats Cache ===" if [[ -f "${CLAUDE_DIR}/stats-cache.json" ]]; then size=$(du -h "${CLAUDE_DIR}/stats-cache.json" | cut -f1) echo "Stats cache: ${size}" echo " (managed by Claude Code, not cleaning)" else echo "Stats cache: not present" fi echo "" # Summary echo "=== Summary ===" if [[ "${DRY_RUN}" == "--dry-run" ]]; then echo "Dry run complete. Run without --dry-run to delete files." else echo -e "${GREEN}โœ“${NC} Cleanup complete!" fi