Skip to content

Instantly share code, notes, and snippets.

@tvd12
Created April 9, 2026 09:01
Show Gist options
  • Select an option

  • Save tvd12/4006839a33f10c3c163aac492e6b403b to your computer and use it in GitHub Desktop.

Select an option

Save tvd12/4006839a33f10c3c163aac492e6b403b to your computer and use it in GitHub Desktop.
uninstall-java-mysql-nginx-certbot.sh
#!/usr/bin/env bash
set -euo pipefail
export LANG=C.UTF-8
export LC_ALL=C.UTF-8
usage() {
cat <<'EOF'
Usage:
sudo bash uninstall-java-mysql-nginx-certbot.sh
What this script does:
- Stops MySQL and Nginx services if they exist
- Removes Java packages, MySQL, Nginx, Certbot, python3-certbot-nginx
- Runs apt autoremove and autoclean
- Removes common leftover configuration directories
Warning:
- This may permanently delete MySQL data and configuration on this server.
- Intended for Ubuntu/Debian systems using apt-get.
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
if [[ "${EUID}" -ne 0 ]]; then
echo "Please run this script as root or with sudo."
exit 1
fi
log() {
printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1"
exit 1
fi
}
stop_service_if_exists() {
local service_name="$1"
if systemctl list-unit-files "${service_name}.service" >/dev/null 2>&1; then
log "Stopping and disabling ${service_name}"
systemctl stop "${service_name}" || true
systemctl disable "${service_name}" || true
fi
}
collect_installed_packages() {
local package_names=("$@")
local installed_packages=()
local package=""
for package in "${package_names[@]}"; do
if dpkg -s "${package}" >/dev/null 2>&1; then
installed_packages+=("${package}")
fi
done
printf '%s\n' "${installed_packages[@]}"
}
collect_installed_java_packages() {
dpkg-query -W -f='${binary:Package}\n' 2>/dev/null | grep -E '^(openjdk-[0-9]+|openjdk|default-jre|default-jdk|oracle-java[0-9]+|temurin-[0-9]+|temurin-[0-9]+-jdk|temurin-[0-9]+-jre|corretto-[0-9]+|corretto-[0-9]+-jdk|corretto-[0-9]+-jre)'
}
purge_packages() {
local packages=(
mysql-server
mysql-client
mysql-common
nginx
nginx-common
certbot
python3-certbot-nginx
)
local installed_packages=()
local java_packages=()
local package=""
while IFS= read -r package; do
[[ -n "${package}" ]] && java_packages+=("${package}")
done < <(collect_installed_java_packages || true)
while IFS= read -r package; do
[[ -n "${package}" ]] && installed_packages+=("${package}")
done < <(collect_installed_packages "${packages[@]}")
if [[ ${#java_packages[@]} -gt 0 ]]; then
installed_packages+=("${java_packages[@]}")
fi
if [[ ${#installed_packages[@]} -eq 0 ]]; then
log "No target packages are currently installed"
return 0
fi
log "Purging packages: ${installed_packages[*]}"
DEBIAN_FRONTEND=noninteractive apt-get purge -y "${installed_packages[@]}"
}
remove_leftovers() {
local paths=(
/usr/lib/jvm
/etc/mysql
/var/lib/mysql
/var/log/mysql
/etc/nginx
/var/log/nginx
/var/www/html
/etc/letsencrypt
/var/lib/letsencrypt
/var/log/letsencrypt
/etc/profile.d/ezyplatform-java.sh
)
for path in "${paths[@]}"; do
if [[ -e "${path}" ]]; then
log "Removing ${path}"
rm -rf "${path}"
fi
done
}
cleanup_packages() {
log "Running apt autoremove"
DEBIAN_FRONTEND=noninteractive apt-get autoremove -y
log "Running apt autoclean"
apt-get autoclean -y
}
print_summary() {
cat <<'EOF'
Uninstall completed.
Removed targets:
- Installed Java packages detected by dpkg
- MySQL packages and common data/config directories
- Nginx packages and common config/log directories
- Certbot packages and common certificate directories
Please review the server if you want to remove any custom files outside the standard paths.
EOF
}
require_command apt-get
require_command systemctl
require_command dpkg
log "Updating apt package index"
apt-get update
stop_service_if_exists mysql
stop_service_if_exists nginx
purge_packages
cleanup_packages
remove_leftovers
print_summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment