CCCPaste

Archlinux cleanup to base system

#!/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 ""
echo "This will KEEP:"
echo "- Base system packages"
echo "- GRUB bootloader"
echo "- Linux kernel and firmware"
echo "- Essential system tools"
echo "- Network tools"
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 
    "networkmanager"
    "wireless_tools"
    "wpa_supplicant"
    
    # 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 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/*

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 "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 "Remove Was succesfull!"