Skip to content

Instantly share code, notes, and snippets.

@nimeshneema
Last active March 27, 2026 07:03
Show Gist options
  • Select an option

  • Save nimeshneema/a7f947a7d1c952d6dc953efcdaaa1822 to your computer and use it in GitHub Desktop.

Select an option

Save nimeshneema/a7f947a7d1c952d6dc953efcdaaa1822 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# ============================================================
# COMPLETE CLEANUP — Removes all traces of Emacs, Java/OpenJDK,
# Coursier, Metals, sbt, and Bloop for a clean-slate reinstall.
# Run as your normal user (it will prompt for sudo as needed).
# ============================================================
set -uo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${RED}=== COMPLETE CLEANUP — Emacs / Java / Scala Toolchain ===${NC}"
echo ""
# ---- 1. Emacs (all possible installation methods) ----
echo -e "${YELLOW}[1/9] Removing Emacs...${NC}"
# Remove PPA-installed Emacs (apt/deb)
sudo apt remove --purge -y emacs emacs-nox emacs-pgtk emacs-lucid emacs-gtk \
emacs-bin-common emacs-common emacs-el 2>/dev/null || true
sudo apt autoremove --purge -y 2>/dev/null || true
# Remove the Emacs PPAs
sudo add-apt-repository --remove -y ppa:ubuntuhandbook1/emacs 2>/dev/null || true
sudo add-apt-repository --remove -y ppa:kelleyk/emacs 2>/dev/null || true
sudo add-apt-repository --remove -y ppa:ubuntu-elisp/ppa 2>/dev/null || true
# Remove Snap-installed Emacs
sudo snap remove emacs 2>/dev/null || true
# Remove Flatpak-installed Emacs
flatpak uninstall -y org.gnu.emacs 2>/dev/null || true
# Remove Emacs if compiled from source (typically installs to /usr/local)
sudo rm -f /usr/local/bin/emacs /usr/local/bin/emacsclient
sudo rm -rf /usr/local/share/emacs
sudo rm -rf /usr/local/libexec/emacs
sudo rm -rf /usr/local/lib/emacs
# Remove all Emacs user configuration and cached packages
rm -rf ~/.emacs.d
rm -rf ~/.emacs
rm -f ~/.emacs.el
rm -rf ~/.config/emacs
# Remove Emacs native compilation cache
rm -rf ~/.cache/emacs
echo -e " ${GREEN}Emacs removed.${NC}"
# ---- 2. Java / OpenJDK (all versions) ----
echo -e "${YELLOW}[2/9] Removing Java / OpenJDK...${NC}"
sudo apt remove --purge -y 'openjdk-*' 2>/dev/null || true
sudo apt autoremove --purge -y 2>/dev/null || true
# Remove manually installed JDKs
sudo rm -rf /usr/local/java
sudo rm -rf /opt/java
# Remove Coursier-managed JVMs
rm -rf ~/.cache/coursier/arc/https/github.com
rm -rf ~/.cache/coursier/jvm
echo -e " ${GREEN}Java removed.${NC}"
# ---- 3. Coursier and everything it installed ----
echo -e "${YELLOW}[3/9] Removing Coursier and installed Scala applications...${NC}"
rm -rf ~/.local/share/coursier
rm -rf ~/.cache/coursier
rm -rf ~/.config/coursier
rm -f ~/cs
rm -f ~/.local/bin/cs
echo -e " ${GREEN}Coursier removed.${NC}"
# ---- 4. Metals specifically ----
echo -e "${YELLOW}[4/9] Removing Metals...${NC}"
rm -f ~/.local/share/coursier/bin/metals
rm -f ~/.local/share/coursier/bin/metals-emacs
rm -f ~/.local/bin/metals
rm -f ~/.local/bin/metals-emacs
rm -f /usr/local/bin/metals
rm -f /usr/local/bin/metals-emacs
echo -e " ${GREEN}Metals removed.${NC}"
# ---- 5. sbt caches and global config ----
echo -e "${YELLOW}[5/9] Removing sbt caches and config...${NC}"
rm -rf ~/.sbt
rm -rf ~/.ivy2
echo -e " ${GREEN}sbt caches removed.${NC}"
# ---- 6. Bloop (Metals' build server) ----
echo -e "${YELLOW}[6/9] Removing Bloop...${NC}"
rm -rf ~/.bloop
rm -rf ~/.cache/bloop
pkill -f bloop 2>/dev/null || true
echo -e " ${GREEN}Bloop removed.${NC}"
# ---- 7. Project-level state (if test project exists) ----
echo -e "${YELLOW}[7/9] Cleaning project-level state...${NC}"
rm -rf ~/scala-test-project/.metals
rm -rf ~/scala-test-project/.bloop
rm -rf ~/scala-test-project/.scala-build
rm -rf ~/scala-test-project/project/metals.sbt
rm -rf ~/scala-test-project/project/project
rm -rf ~/scala-test-project/target
echo -e " ${GREEN}Project state cleaned.${NC}"
# ---- 8. Clean up shell profile entries ----
echo -e "${YELLOW}[8/9] Checking shell profiles for stale entries...${NC}"
echo ""
FOUND_STALE=0
while IFS= read -r line; do
if [ -n "$line" ]; then
echo -e " ${RED}PLEASE DELETE >>> ${line}${NC}"
FOUND_STALE=1
fi
done < <(grep -n 'JAVA_HOME' ~/.bashrc ~/.profile ~/.bash_profile ~/.zshrc 2>/dev/null)
while IFS= read -r line; do
if [ -n "$line" ]; then
echo -e " ${RED}PLEASE DELETE >>> ${line}${NC}"
FOUND_STALE=1
fi
done < <(grep -n 'coursier' ~/.bashrc ~/.profile ~/.bash_profile ~/.zshrc 2>/dev/null)
if [ "$FOUND_STALE" -eq 0 ]; then
echo -e " ${GREEN}No stale entries found. All clean.${NC}"
else
echo ""
echo -e " ${YELLOW}Open the file(s) shown above and delete the indicated line(s).${NC}"
echo -e " ${YELLOW}The format is: filename:line_number:line_content${NC}"
fi
# ---- 9. Refresh and verify ----
echo -e "${YELLOW}[9/9] Refreshing package lists and verifying...${NC}"
sudo apt update -qq
echo ""
echo -e "${GREEN}--- Verification ---${NC}"
echo "Emacs: $(which emacs 2>/dev/null || echo 'NOT FOUND (good)')"
echo "Java: $(which java 2>/dev/null || echo 'NOT FOUND (good)')"
echo "Coursier: $(which cs 2>/dev/null || echo 'NOT FOUND (good)')"
echo "Metals: $(which metals 2>/dev/null || echo 'NOT FOUND (good)')"
echo "sbt: $(which sbt 2>/dev/null || echo 'NOT FOUND (good)')"
echo "Bloop: $(which bloop 2>/dev/null || echo 'NOT FOUND (good)')"
echo ""
echo "Emacs config: $(ls -d ~/.emacs.d 2>/dev/null || echo 'NOT FOUND (good)')"
echo "JAVA_HOME: ${JAVA_HOME:-(not set, good)}"
echo ""
echo -e "${GREEN}=== Cleanup complete ===${NC}"
echo ""
echo "Open a NEW terminal (or log out and back in) before reinstalling."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment