Ratiocine
Log in
bash 0 upvotesยท 0 copies

master cleanup

RatiocineยทJul 5, 2026
#!/bin/bash

# Clear terminal screen for a clean presentation layout
clear

show_menu() {
    clear
    echo "========================================================"
    echo "๐Ÿง™โ€โ™‚๏ธ THE ULTIMATE LINUX MASTER CLEANUP WIZARD"
    echo "========================================================"
    echo "1) ๐Ÿ“ฆ Manage & Purge System Apps (APT Checklist)"
    echo "2) ๐Ÿš€ Manage & Purge Container Apps (FLATPAK)"
    echo "3) ๐Ÿงน Sweep System Caches & Orphaned Runtimes Only"
    echo "4) ๐Ÿšช Exit Wizard"
    echo "========================================================"
    read -p "Select an action [1-4]: " CHOICE
    case $CHOICE in
        1) cleanup_apt ;;
        2) cleanup_flatpak ;;
        3) quick_sweep ;;
        4) echo "๐Ÿ‘‹ Goodbye! Keep your system clean."; exit 0 ;;
        *) echo -e "โŒ Invalid selection.\n"; sleep 1; show_menu ;;
    esac
}

cleanup_apt() {
    clear
    echo "========================================================"
    echo "๐Ÿ“ฆ SYSTEM PACKAGE CLEANUP (APT MANAGED - CHECKLIST)"
    echo "========================================================"
    
    # 1. Generate the clean, filtered list of user-installed apps
    echo "๐Ÿ”„ Generating application checklist..."
    MAPFILE=()
    while IFS= read -r app; do
        # Store app name, a blank description, and set default state to 'OFF'
        MAPFILE+=("$app" "" "OFF")
    done < <(apt-mark showmanual | grep -vE '^(lib|linux-|firmware-|base-|ubuntu-|python3-|fonts-|language-pack-|locales|mythes-|coreutils|bash|apt|adduser|login|cron|curl|less|logrotate|logsave|mount|lsb-|console-setup|bsdutils|mawk|media-types|chntpw|chrony)' | sort)

    # 2. Open the visual whiptail checklist interface
    CHOICES=$(whiptail --title "App Extermination Checklist" \
                       --checklist "Use ARROW KEYS to scroll.\nPress SPACEBAR to mark [X] the apps you want to delete.\nPress ENTER when finished." \
                       20 70 10 \
                       "${MAPFILE[@]}" 3>&1 1>&2 2>&3)

    # Exit cleanly if the user cancels or selects nothing
    if [ -z "$CHOICES" ]; then
        echo "โŒ No applications selected for removal."
        read -p "Press Enter to return to the Main Menu..." CONF; show_menu
        return
    fi

    # 3. Loop through every single app marked with an [X]
    # Strip quotes from the choices string array
    CHOICES=$(echo "$CHOICES" | tr -d '"')
    
    for TARGET in $CHOICES; do
        clear
        echo "========================================================"
        echo "๐Ÿ”ฅ TARGET ACQUIRED: $TARGET"
        echo "========================================================"
        
        # Hunt paths specific to this targeted app loop
        PATHS=(
            "$HOME/.config/$TARGET" "$HOME/.config/${TARGET,,}"
            "$HOME/.cache/$TARGET" "$HOME/.cache/${TARGET,,}"
            "$HOME/.$TARGET" "$HOME/.${TARGET,,}"
            "$HOME/.local/share/$TARGET" "$HOME/.local/share/${TARGET,,}"
            "/var/log/$TARGET" "/etc/$TARGET"
        )

        FOUND_PATHS=()
        for p in "${PATHS[@]}"; do
            if [ -e "$p" ] || [ -d "$p" ]; then FOUND_PATHS+=("$p"); fi
        done

        echo -e "\nโš ๏ธ [STEP 1] SYSTEM PACKAGE PURGE"
        read -p "โ“ Run 'apt purge' to completely wipe core $TARGET files? (y/N): " PURGE_CONFIRM
        if [[ "$PURGE_CONFIRM" =~ ^[Yy]$ ]]; then
            sudo apt purge -y "$TARGET"
            sudo apt autoremove -y
        else
            echo "โญ๏ธ  Skipping core package removal."
        fi

        echo -e "\n๐Ÿ“ [STEP 2] USER SPACE CONFIGS & CACHES"
        if [ ${#FOUND_PATHS[@]} -eq 0 ]; then
            echo "โœ… No loose configuration or cache folders found in your home directory."
        else
            echo "Found the following residual paths for $TARGET. Choose item-by-item:"
            for folder in "${FOUND_PATHS[@]}"; do
                read -p "โ“ Delete folder: $folder? (y/N): " FOLDER_CONFIRM
                if [[ "$FOLDER_CONFIRM" =~ ^[Yy]$ ]]; then
                    rm -rf "$folder"
                    echo "๐Ÿ—‘๏ธ  Deleted: $folder"
                else
                    echo "๐Ÿ›ก๏ธ  Kept: $folder"
                fi
                echo "----------------------------"
            done
        fi
        echo "========================================================"
        echo "๐Ÿ Finished processing $TARGET."
        sleep 1
    done

    read -p "All selected apps processed! Press Enter to return to Main Menu..." CONF; show_menu
}

cleanup_flatpak() {
    clear
    echo "========================================================"
    echo "๐Ÿš€ CONTAINER APPLICATION CLEANUP (FLATPAK MANAGED)"
    echo "========================================================"
    
    if ! command -v flatpak &> /dev/null; then
        echo "โŒ Flatpak is not installed on this system framework."
        sleep 2; show_menu; return
    fi

    echo -e "\nInstalled Flatpak Applications:"
    echo "--------------------------------------------------------"
    flatpak list --app --columns=application,name | column -t
    echo "--------------------------------------------------------"

    echo -e "\n๐Ÿ” Which Flatpak app do you want to target?"
    read -p "Enter exact Application ID (or press Enter to return to menu): " TARGET

    if [ -z "$TARGET" ]; then
        show_menu; return
    fi

    if ! flatpak list --app | grep -q "$TARGET"; then
        echo "โŒ App ID '$TARGET' not found."
        sleep 2; cleanup_flatpak; return
    fi

    echo -e "\nโš ๏ธ [STEP 1] CORE APP & DATA PURGE"
    read -p "โ“ Uninstall $TARGET and WIPE all its hidden sandbox container user data? (y/N): " UNINSTALL_CONFIRM
    if [[ "$UNINSTALL_CONFIRM" =~ ^[Yy]$ ]]; then
        flatpak uninstall --delete-data -y "$TARGET"
        echo "โœจ Application container data completely purged."
    else
        echo "โญ๏ธ  Skipping application removal."
    fi

    echo -e "\n๐Ÿงน [STEP 2] SHARED SYSTEM RUNTIMES CLEANUP"
    flatpak_runtime_sweep
    read -p "Press Enter to return to the Main Menu..." CONF; show_menu
}

flatpak_runtime_sweep() {
    if command -v flatpak &> /dev/null; then
        UNUSED_COUNT=$(flatpak uninstall --unused --dry-run | wc -l)
        if [ "$UNUSED_COUNT" -gt 1 ]; then
            read -p "โ“ Found orphaned runtime blocks. Purge these heavy shared assets? (y/N): " RUNTIME_CONFIRM
            if [[ "$RUNTIME_CONFIRM" =~ ^[Yy]$ ]]; then
                flatpak uninstall --unused -y
            fi
        else
            echo "โœ… No orphaned shared runtimes detected."
        fi
    fi
}

quick_sweep() {
    clear
    echo "========================================================"
    echo "๐Ÿงน QUICK SYSTEM SWEEP (GLOBAL CACHE & PACKAGE CLEANUP)"
    echo "========================================================"
    
    echo "1. Cleaning APT download cache..."
    sudo apt-get clean
    sudo apt-get autoclean
    
    if command -v flatpak &> /dev/null; then
        echo -e "\n2. Sweeping orphaned Flatpak runtimes..."
        flatpak_runtime_sweep
        echo -e "\n3. Dropping transient sandbox download folders..."
        rm -rf ~/.var/app/*/cache/*
    fi
    
    if [ -d "$HOME/.cache/uv" ]; then
        echo -e "\n4. Wiping modular Python 'uv' build cache..."
        rm -rf ~/.cache/uv
    fi

    echo -e "\nโœจ All global caches safely streamlined!"
    read -p "Press Enter to return to the Main Menu..." CONF; show_menu
}

# Launch the wizard menu loop automatically on execution
while true; do
    show_menu
done
Sign up to save