CCCPaste

Archlinux super aggressive cleanup

#!/bin/bash

# My Arch bro....Do not run this without my permission bro!
# This script removes all packages except base system and GRUB bootloader

set -e  # Exit on any error

echo "=================================="
echo "Arch Linux System Cleanup Script"
echo "=================================="
echo ""
echo "WARNING: This will remove:"
echo "- GNOME Desktop Environment"
echo "- KDE Plasma Desktop Environment" 
echo "- All applications and games"
echo "- Development tools (keeping base-devel)"
echo "- Most user applications"
echo "- NetworkManager and wireless tools"
echo "- All network tools except dhcpcd (ethernet)"
echo ""
echo "This will KEEP:"
echo "- Base system packages"
echo "- GRUB bootloader"
echo "- Linux kernel and firmware"
echo "- Essential system tools"
echo "- dhcpcd (for ethernet reinstallation)"
echo "- Basic development tools"
echo ""

read -p "Are you ABSOLUTELY sure you want to continue? (type 'YES' to proceed): " confirmation

if [ "$confirmation" != "YES" ]; then
    echo "Operation cancelled."
    exit 1
fi

echo ""
echo "Starting cleanup process..."

# Essential packages to keep (  EDIT based in you archdesktop, I assumed some stuff here like eg grub in use)
KEEP_PACKAGES=(
    # Base system....edit? NO!
    "base"
    "base-devel"
    
    # Bootloader, i dont have to remind you here
    "grub"
    "os-prober"
    "efibootmgr"
    
    # Kernel and firmware, edit this and get cooked
    "linux"
    "linux-headers"
    "linux-firmware"
    "intel-ucode"
    
    # Essential system tools wont prevent terminal runs if edited
    "nano"
    "git"
    "htop"
    "ncdu"
    
    # Network - ONLY ethernet tool for reinstallation
    "dhcpcd"
    
    # Audio , edit based on your audio setup
    "alsa-utils"
    "pipewire-alsa"
    "pipewire-pulse"
    
    # File systems
    "dosfstools"
    "ntfs-3g"
    "mtools"
    
    # Package management
    "pacman-contrib"
    "reflector"
    "yay"
    
    # Basic utilities 
    "scrot"
    "zsh"
    
    # Hardware support, could remove these too
    "bluez"
    "bluez-utils"
    "libva-intel-driver"
    "libva-utils"
    "libvdpau-va-gl"
    "vulkan-intel"
    
    # Basic development, could remove these okay
    "python-virtualenv"
    "rustup"
    "npm"
    
    # Fonts (minimal)
    "ttf-dejavu"
    "ttf-liberation"
    "noto-fonts"
    
    # System integration
    "xdg-utils"
    "xdg-user-dirs"
)

echo "Packages that will be KEPT:"
printf '%s\n' "${KEEP_PACKAGES[@]}"
echo ""

# Create temp file with packages to keep
KEEP_FILE=$(mktemp)
printf '%s\n' "${KEEP_PACKAGES[@]}" | sort > "$KEEP_FILE"

# Get list of explicitly installed packages
INSTALLED_FILE=$(mktemp)
pacman -Qqe | sort > "$INSTALLED_FILE"

# Find packages to remove (installed packages minus packages to keep)
REMOVE_FILE=$(mktemp)
comm -23 "$INSTALLED_FILE" "$KEEP_FILE" > "$REMOVE_FILE"

echo "Packages that will be REMOVED:"
cat "$REMOVE_FILE"
echo ""
echo "Total packages to remove: $(wc -l < "$REMOVE_FILE")"
echo ""

read -p "Proceed with removal? (type 'YES' to continue): " final_confirmation

if [ "$final_confirmation" != "YES" ]; then
    echo "Operation cancelled."
    rm "$KEEP_FILE" "$INSTALLED_FILE" "$REMOVE_FILE"
    exit 1
fi

echo "Starting package removal..."

# Read packages to remove and remove them in batches to avoid command line length limits
BATCH_SIZE=50
BATCH_COUNT=0
BATCH_PACKAGES=()

while IFS= read -r package; do
    BATCH_PACKAGES+=("$package")
    
    if [ ${#BATCH_PACKAGES[@]} -eq $BATCH_SIZE ]; then
        BATCH_COUNT=$((BATCH_COUNT + 1))
        echo ""
        echo "Removing batch $BATCH_COUNT (${#BATCH_PACKAGES[@]} packages)..."
        
        # Remove packages without dependencies to avoid removing needed packages
        if sudo pacman -Rdd --noconfirm "${BATCH_PACKAGES[@]}" 2>/dev/null; then
            echo "Batch $BATCH_COUNT removed successfully"
        else
            echo "Some packages in batch $BATCH_COUNT could not be removed (likely dependencies)"
        fi
        
        BATCH_PACKAGES=()
    fi
done < "$REMOVE_FILE"

# Remove remaining packages in final batch
if [ ${#BATCH_PACKAGES[@]} -gt 0 ]; then
    BATCH_COUNT=$((BATCH_COUNT + 1))
    echo ""
    echo "Removing final batch $BATCH_COUNT (${#BATCH_PACKAGES[@]} packages)..."
    
    if sudo pacman -Rdd --noconfirm "${BATCH_PACKAGES[@]}" 2>/dev/null; then
        echo "Final batch removed successfully"
    else
        echo "Some packages in final batch could not be removed (likely dependencies)"
    fi
fi

echo ""
echo "Package removal phase completed."

# Clean up orphaned packages
echo ""
echo "Removing orphaned packages..."
if orphans=$(pacman -Qtdq 2>/dev/null); then
    echo "Found orphaned packages:"
    echo "$orphans"
    sudo pacman -Rns --noconfirm $orphans
    echo "Orphaned packages removed."
else
    echo "No orphaned packages found."
fi

# Clean package cache
echo ""
echo "Cleaning package cache..."
sudo paccache -r
sudo paccache -ruk0

# Clean all package cache files
echo "Removing all cached package files..."
sudo pacman -Scc --noconfirm

# Clean system logs and temporary files
echo ""
echo "Cleaning system files..."
sudo journalctl --vacuum-time=1week
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*

# Clean user cache (be careful with this)
echo "Cleaning user cache..."
rm -rf ~/.cache/*

# Clean additional cache locations
echo "Cleaning additional system caches..."
sudo rm -rf /var/cache/* 2>/dev/null || true
sudo rm -rf ~/.local/share/Trash/* 2>/dev/null || true

# Clean NetworkManager and network-related cache
echo "Cleaning network-related cache..."
sudo rm -rf /var/lib/NetworkManager/* 2>/dev/null || true
sudo rm -rf /etc/NetworkManager/system-connections/* 2>/dev/null || true
sudo rm -rf ~/.config/NetworkManager/* 2>/dev/null || true

# Clean systemd network cache
sudo rm -rf /var/lib/systemd/network/* 2>/dev/null || true

# Clean thumbnail cache
echo "Cleaning thumbnail cache..."
rm -rf ~/.thumbnails/* 2>/dev/null || true
rm -rf ~/.cache/thumbnails/* 2>/dev/null || true

# Clean browser caches if they exist
echo "Cleaning browser caches..."
rm -rf ~/.mozilla/firefox/*/cache* 2>/dev/null || true
rm -rf ~/.cache/mozilla/* 2>/dev/null || true
rm -rf ~/.config/google-chrome/*/Cache* 2>/dev/null || true
rm -rf ~/.cache/google-chrome/* 2>/dev/null || true
rm -rf ~/.cache/chromium/* 2>/dev/null || true

# Clean pip cache
rm -rf ~/.cache/pip/* 2>/dev/null || true

# Clean npm cache
rm -rf ~/.npm/_cacache/* 2>/dev/null || true

# Clean yarn cache
rm -rf ~/.cache/yarn/* 2>/dev/null || true

# Clean Rust cargo cache
rm -rf ~/.cargo/registry/cache/* 2>/dev/null || true

echo ""
echo "=================================="
echo "Cleanup completed!"
echo "=================================="
echo ""
echo "Summary of remaining packages:"
pacman -Qe | wc -l | xargs echo "Explicitly installed packages:"
echo ""
echo "System has been cleaned to minimal state."
echo "NetworkManager and wireless tools have been removed."
echo "dhcpcd (ethernet tool) has been kept for reinstallation."
echo ""
echo "To connect via ethernet after reboot:"
echo "sudo dhcpcd "
echo "Find your interface with: ip link"
echo ""
echo "You may want to reboot to ensure everything works correctly."
echo ""
echo "To verify GRUB is still working:"
echo "sudo grub-mkconfig -o /boot/grub/grub.cfg"
echo ""

# Clean up temp files
rm "$KEEP_FILE" "$INSTALLED_FILE" "$REMOVE_FILE"

echo "Removal was successful!"