Tetragrammaton

Archlinux super aggressive cleanup

Nov 1st, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # My Arch bro....Do not run this without my permission bro!
  4. # This script removes all packages except base system and GRUB bootloader
  5.  
  6. set -e  # Exit on any error
  7.  
  8. echo "=================================="
  9. echo "Arch Linux System Cleanup Script"
  10. echo "=================================="
  11. echo ""
  12. echo "WARNING: This will remove:"
  13. echo "- GNOME Desktop Environment"
  14. echo "- KDE Plasma Desktop Environment"
  15. echo "- All applications and games"
  16. echo "- Development tools (keeping base-devel)"
  17. echo "- Most user applications"
  18. echo "- NetworkManager and wireless tools"
  19. echo "- All network tools except dhcpcd (ethernet)"
  20. echo ""
  21. echo "This will KEEP:"
  22. echo "- Base system packages"
  23. echo "- GRUB bootloader"
  24. echo "- Linux kernel and firmware"
  25. echo "- Essential system tools"
  26. echo "- dhcpcd (for ethernet reinstallation)"
  27. echo "- Basic development tools"
  28. echo ""
  29.  
  30. read -p "Are you ABSOLUTELY sure you want to continue? (type 'YES' to proceed): " confirmation
  31.  
  32. if [ "$confirmation" != "YES" ]; then
  33.     echo "Operation cancelled."
  34.     exit 1
  35. fi
  36.  
  37. echo ""
  38. echo "Starting cleanup process..."
  39.  
  40. # Essential packages to keep (  EDIT based in you archdesktop, I assumed some stuff here like eg grub in use)
  41. KEEP_PACKAGES=(
  42.     # Base system....edit? NO!
  43.     "base"
  44.     "base-devel"
  45.    
  46.     # Bootloader, i dont have to remind you here
  47.     "grub"
  48.     "os-prober"
  49.     "efibootmgr"
  50.    
  51.     # Kernel and firmware, edit this and get cooked
  52.     "linux"
  53.     "linux-headers"
  54.     "linux-firmware"
  55.     "intel-ucode"
  56.    
  57.     # Essential system tools wont prevent terminal runs if edited
  58.     "nano"
  59.     "git"
  60.     "htop"
  61.     "ncdu"
  62.    
  63.     # Network - ONLY ethernet tool for reinstallation
  64.     "dhcpcd"
  65.    
  66.     # Audio , edit based on your audio setup
  67.     "alsa-utils"
  68.     "pipewire-alsa"
  69.     "pipewire-pulse"
  70.    
  71.     # File systems
  72.     "dosfstools"
  73.     "ntfs-3g"
  74.     "mtools"
  75.    
  76.     # Package management
  77.     "pacman-contrib"
  78.     "reflector"
  79.     "yay"
  80.    
  81.     # Basic utilities
  82.     "scrot"
  83.     "zsh"
  84.    
  85.     # Hardware support, could remove these too
  86.     "bluez"
  87.     "bluez-utils"
  88.     "libva-intel-driver"
  89.     "libva-utils"
  90.     "libvdpau-va-gl"
  91.     "vulkan-intel"
  92.    
  93.     # Basic development, could remove these okay
  94.     "python-virtualenv"
  95.     "rustup"
  96.     "npm"
  97.    
  98.     # Fonts (minimal)
  99.     "ttf-dejavu"
  100.     "ttf-liberation"
  101.     "noto-fonts"
  102.    
  103.     # System integration
  104.     "xdg-utils"
  105.     "xdg-user-dirs"
  106. )
  107.  
  108. echo "Packages that will be KEPT:"
  109. printf '%s\n' "${KEEP_PACKAGES[@]}"
  110. echo ""
  111.  
  112. # Create temp file with packages to keep
  113. KEEP_FILE=$(mktemp)
  114. printf '%s\n' "${KEEP_PACKAGES[@]}" | sort > "$KEEP_FILE"
  115.  
  116. # Get list of explicitly installed packages
  117. INSTALLED_FILE=$(mktemp)
  118. pacman -Qqe | sort > "$INSTALLED_FILE"
  119.  
  120. # Find packages to remove (installed packages minus packages to keep)
  121. REMOVE_FILE=$(mktemp)
  122. comm -23 "$INSTALLED_FILE" "$KEEP_FILE" > "$REMOVE_FILE"
  123.  
  124. echo "Packages that will be REMOVED:"
  125. cat "$REMOVE_FILE"
  126. echo ""
  127. echo "Total packages to remove: $(wc -l < "$REMOVE_FILE")"
  128. echo ""
  129.  
  130. read -p "Proceed with removal? (type 'YES' to continue): " final_confirmation
  131.  
  132. if [ "$final_confirmation" != "YES" ]; then
  133.     echo "Operation cancelled."
  134.     rm "$KEEP_FILE" "$INSTALLED_FILE" "$REMOVE_FILE"
  135.     exit 1
  136. fi
  137.  
  138. echo "Starting package removal..."
  139.  
  140. # Read packages to remove and remove them in batches to avoid command line length limits
  141. BATCH_SIZE=50
  142. BATCH_COUNT=0
  143. BATCH_PACKAGES=()
  144.  
  145. while IFS= read -r package; do
  146.     BATCH_PACKAGES+=("$package")
  147.    
  148.     if [ ${#BATCH_PACKAGES[@]} -eq $BATCH_SIZE ]; then
  149.         BATCH_COUNT=$((BATCH_COUNT + 1))
  150.         echo ""
  151.         echo "Removing batch $BATCH_COUNT (${#BATCH_PACKAGES[@]} packages)..."
  152.        
  153.         # Remove packages without dependencies to avoid removing needed packages
  154.         if sudo pacman -Rdd --noconfirm "${BATCH_PACKAGES[@]}" 2>/dev/null; then
  155.             echo "Batch $BATCH_COUNT removed successfully"
  156.         else
  157.             echo "Some packages in batch $BATCH_COUNT could not be removed (likely dependencies)"
  158.         fi
  159.        
  160.         BATCH_PACKAGES=()
  161.     fi
  162. done < "$REMOVE_FILE"
  163.  
  164. # Remove remaining packages in final batch
  165. if [ ${#BATCH_PACKAGES[@]} -gt 0 ]; then
  166.     BATCH_COUNT=$((BATCH_COUNT + 1))
  167.     echo ""
  168.     echo "Removing final batch $BATCH_COUNT (${#BATCH_PACKAGES[@]} packages)..."
  169.    
  170.     if sudo pacman -Rdd --noconfirm "${BATCH_PACKAGES[@]}" 2>/dev/null; then
  171.         echo "Final batch removed successfully"
  172.     else
  173.         echo "Some packages in final batch could not be removed (likely dependencies)"
  174.     fi
  175. fi
  176.  
  177. echo ""
  178. echo "Package removal phase completed."
  179.  
  180. # Clean up orphaned packages
  181. echo ""
  182. echo "Removing orphaned packages..."
  183. if orphans=$(pacman -Qtdq 2>/dev/null); then
  184.     echo "Found orphaned packages:"
  185.     echo "$orphans"
  186.     sudo pacman -Rns --noconfirm $orphans
  187.     echo "Orphaned packages removed."
  188. else
  189.     echo "No orphaned packages found."
  190. fi
  191.  
  192. # Clean package cache
  193. echo ""
  194. echo "Cleaning package cache..."
  195. sudo paccache -r
  196. sudo paccache -ruk0
  197.  
  198. # Clean all package cache files
  199. echo "Removing all cached package files..."
  200. sudo pacman -Scc --noconfirm
  201.  
  202. # Clean system logs and temporary files
  203. echo ""
  204. echo "Cleaning system files..."
  205. sudo journalctl --vacuum-time=1week
  206. sudo rm -rf /tmp/*
  207. sudo rm -rf /var/tmp/*
  208.  
  209. # Clean user cache (be careful with this)
  210. echo "Cleaning user cache..."
  211. rm -rf ~/.cache/*
  212.  
  213. # Clean additional cache locations
  214. echo "Cleaning additional system caches..."
  215. sudo rm -rf /var/cache/* 2>/dev/null || true
  216. sudo rm -rf ~/.local/share/Trash/* 2>/dev/null || true
  217.  
  218. # Clean NetworkManager and network-related cache
  219. echo "Cleaning network-related cache..."
  220. sudo rm -rf /var/lib/NetworkManager/* 2>/dev/null || true
  221. sudo rm -rf /etc/NetworkManager/system-connections/* 2>/dev/null || true
  222. sudo rm -rf ~/.config/NetworkManager/* 2>/dev/null || true
  223.  
  224. # Clean systemd network cache
  225. sudo rm -rf /var/lib/systemd/network/* 2>/dev/null || true
  226.  
  227. # Clean thumbnail cache
  228. echo "Cleaning thumbnail cache..."
  229. rm -rf ~/.thumbnails/* 2>/dev/null || true
  230. rm -rf ~/.cache/thumbnails/* 2>/dev/null || true
  231.  
  232. # Clean browser caches if they exist
  233. echo "Cleaning browser caches..."
  234. rm -rf ~/.mozilla/firefox/*/cache* 2>/dev/null || true
  235. rm -rf ~/.cache/mozilla/* 2>/dev/null || true
  236. rm -rf ~/.config/google-chrome/*/Cache* 2>/dev/null || true
  237. rm -rf ~/.cache/google-chrome/* 2>/dev/null || true
  238. rm -rf ~/.cache/chromium/* 2>/dev/null || true
  239.  
  240. # Clean pip cache
  241. rm -rf ~/.cache/pip/* 2>/dev/null || true
  242.  
  243. # Clean npm cache
  244. rm -rf ~/.npm/_cacache/* 2>/dev/null || true
  245.  
  246. # Clean yarn cache
  247. rm -rf ~/.cache/yarn/* 2>/dev/null || true
  248.  
  249. # Clean Rust cargo cache
  250. rm -rf ~/.cargo/registry/cache/* 2>/dev/null || true
  251.  
  252. echo ""
  253. echo "=================================="
  254. echo "Cleanup completed!"
  255. echo "=================================="
  256. echo ""
  257. echo "Summary of remaining packages:"
  258. pacman -Qe | wc -l | xargs echo "Explicitly installed packages:"
  259. echo ""
  260. echo "System has been cleaned to minimal state."
  261. echo "NetworkManager and wireless tools have been removed."
  262. echo "dhcpcd (ethernet tool) has been kept for reinstallation."
  263. echo ""
  264. echo "To connect via ethernet after reboot:"
  265. echo "sudo dhcpcd <interface_name>"
  266. echo "Find your interface with: ip link"
  267. echo ""
  268. echo "You may want to reboot to ensure everything works correctly."
  269. echo ""
  270. echo "To verify GRUB is still working:"
  271. echo "sudo grub-mkconfig -o /boot/grub/grub.cfg"
  272. echo ""
  273.  
  274. # Clean up temp files
  275. rm "$KEEP_FILE" "$INSTALLED_FILE" "$REMOVE_FILE"
  276.  
  277. echo "Removal was successful!"
Advertisement
Add Comment
Please, Sign In to add comment