diff --git a/lib/check/health_json.sh b/lib/check/health_json.sh index 2292f12..7428384 100644 --- a/lib/check/health_json.sh +++ b/lib/check/health_json.sh @@ -127,6 +127,7 @@ EOF items+=('recent_items|Recent Items|Clear recent apps/documents/servers lists|true') items+=('log_cleanup|Diagnostics Cleanup|Purge old diagnostic & crash logs|true') items+=('startup_cache|Startup Cache Rebuild|Rebuild kext caches & prelinked kernel|true') + items+=('spotlight_cache_cleanup|Spotlight Cache Cleanup|Clear CoreSpotlight user cache|false') # Output items as JSON local first=true diff --git a/lib/clean/caches.sh b/lib/clean/caches.sh index e176e87..966ad6c 100644 --- a/lib/clean/caches.sh +++ b/lib/clean/caches.sh @@ -216,8 +216,6 @@ clean_project_caches() { } # Clean Spotlight user caches -# Cleans CoreSpotlight index cache and Spotlight saved state -# System Spotlight index (/System/Volumes/Data/.Spotlight-V100) is never touched clean_spotlight_caches() { local cleaned_size=0 local cleaned_count=0 diff --git a/lib/clean/user.sh b/lib/clean/user.sh index 0f34842..e722f7c 100644 --- a/lib/clean/user.sh +++ b/lib/clean/user.sh @@ -80,8 +80,7 @@ clean_macos_system_caches() { safe_clean ~/Library/Saved\ Application\ State/* "Saved application states" safe_clean ~/Library/Caches/com.apple.spotlight "Spotlight cache" - # Clean Spotlight user caches (CoreSpotlight can grow very large) - clean_spotlight_caches + # MOVED: Spotlight cache cleanup moved to optimize command safe_clean ~/Library/Caches/com.apple.photoanalysisd "Photo analysis cache" safe_clean ~/Library/Caches/com.apple.akd "Apple ID cache" diff --git a/lib/manage/whitelist.sh b/lib/manage/whitelist.sh index fd8a432..3e9861d 100755 --- a/lib/manage/whitelist.sh +++ b/lib/manage/whitelist.sh @@ -163,6 +163,7 @@ TouchID sudo check|check_touchid|config_check Rosetta 2 check|check_rosetta|config_check Git configuration check|check_git_config|config_check Login items check|check_login_items|config_check +Spotlight cache cleanup|spotlight_cache|system_optimization EOF } diff --git a/lib/optimize/tasks.sh b/lib/optimize/tasks.sh index 69bc3a3..66a3e09 100644 --- a/lib/optimize/tasks.sh +++ b/lib/optimize/tasks.sh @@ -410,6 +410,56 @@ opt_fix_broken_configs() { fi } +# Clean Spotlight user caches +opt_spotlight_cache_cleanup() { + # Check whitelist + if is_whitelisted "spotlight_cache"; then + echo -e "${GRAY}${ICON_SUCCESS}${NC} Spotlight cache cleanup (whitelisted)" + return 0 + fi + + echo -e "${BLUE}${ICON_ARROW}${NC} Cleaning Spotlight user caches..." + + local cleaned_count=0 + local total_size_kb=0 + + # CoreSpotlight user cache (can grow very large) + local spotlight_cache="$HOME/Library/Metadata/CoreSpotlight" + if [[ -d "$spotlight_cache" ]]; then + local size_kb=$(get_path_size_kb "$spotlight_cache") + if [[ "$size_kb" -gt 0 ]]; then + local size_human=$(bytes_to_human "$((size_kb * 1024))") + if safe_remove "$spotlight_cache" true; then + echo -e " ${GREEN}${ICON_SUCCESS}${NC} CoreSpotlight cache ${GREEN}($size_human)${NC}" + ((cleaned_count++)) + ((total_size_kb += size_kb)) + fi + fi + fi + + # Spotlight saved application state + local spotlight_state="$HOME/Library/Saved Application State/com.apple.spotlight.Spotlight.savedState" + if [[ -d "$spotlight_state" ]]; then + local size_kb=$(get_path_size_kb "$spotlight_state") + if [[ "$size_kb" -gt 0 ]]; then + local size_human=$(bytes_to_human "$((size_kb * 1024))") + if safe_remove "$spotlight_state" true; then + echo -e " ${GREEN}${ICON_SUCCESS}${NC} Spotlight state ${GREEN}($size_human)${NC}" + ((cleaned_count++)) + ((total_size_kb += size_kb)) + fi + fi + fi + + if [[ $cleaned_count -gt 0 ]]; then + local total_human=$(bytes_to_human "$((total_size_kb * 1024))") + echo -e "${GREEN}${ICON_SUCCESS}${NC} Cleaned $cleaned_count items ${GREEN}($total_human)${NC}" + echo -e "${YELLOW}${ICON_WARNING}${NC} System settings may require logout/restart to display correctly" + else + echo -e "${GREEN}${ICON_SUCCESS}${NC} No Spotlight caches to clean" + fi +} + # Execute optimization by action name execute_optimization() { local action="$1" @@ -430,6 +480,7 @@ execute_optimization() { local_snapshots) opt_local_snapshots ;; developer_cleanup) opt_developer_cleanup ;; fix_broken_configs) opt_fix_broken_configs ;; + spotlight_cache_cleanup) opt_spotlight_cache_cleanup ;; *) echo -e "${RED}${ICON_ERROR}${NC} Unknown action: $action" return 1