How to Completely Uninstall MySQL Server on Debian: A Clean Removal Guide for Developers


2 views

Many developers find themselves in a frustrating situation when trying to reinstall MySQL on Debian systems. The standard apt-get remove or even --purge commands often leave behind configuration files and data directories, causing problems when attempting a fresh installation.

The Debian package management system is designed to preserve user data and configuration files by default. While this is generally a good practice, it becomes problematic when:

  • Your MySQL configuration is corrupted
  • You need to reset to default settings
  • You're preparing a system for production deployment
  • You're troubleshooting persistent MySQL issues

Here's the definitive method to completely remove MySQL Server from a Debian system:

# First, stop the MySQL service
sudo systemctl stop mysql

# Remove MySQL packages and dependencies
sudo apt-get remove --purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

# Remove configuration files
sudo rm -rf /etc/mysql /var/lib/mysql

# Remove log files
sudo rm -rf /var/log/mysql

# Clean up any remaining dependencies
sudo apt-get autoremove
sudo apt-get autoclean

After executing these commands, verify that no MySQL components remain:

# Check for remaining MySQL processes
ps aux | grep mysql

# Check for remaining MySQL files
sudo find / -name "*mysql*"

# Check package status
dpkg -l | grep mysql

For systems with custom configurations or multiple MySQL instances:

# If you've used custom data directories
sudo rm -rf /your/custom/data/directory

# Remove MySQL user and group (if they exist)
sudo deluser mysql
sudo delgroup mysql

Once you've performed the complete removal, you can install a fresh MySQL instance:

sudo apt-get update
sudo apt-get install mysql-server

This will now install MySQL as if it were a completely new installation, with default configuration files and a fresh data directory.

  • Never remove MySQL packages while the service is running
  • Be cautious with rm -rf commands - double check paths
  • Back up important databases before uninstalling
  • Remember that this process is irreversible

Many developers find themselves stuck in a frustrating loop when trying to completely remove MySQL from Debian systems. The standard apt-get remove or even --purge options often leave behind configuration files and database directories, causing problems during reinstallation.

The Debian package system is designed to preserve user data during uninstallation, which includes MySQL's:

  • Configuration files in /etc/mysql
  • Database files in /var/lib/mysql
  • Log files in /var/log/mysql
  • Temporary files in /var/run/mysqld

Here's the definitive way to completely wipe MySQL from your Debian system:

# Stop MySQL service first
sudo systemctl stop mysql

# Remove MySQL packages
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

# Remove all MySQL files and directories
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql /var/run/mysqld

# Clean up any remaining dependencies
sudo apt-get autoremove
sudo apt-get autoclean

After executing these commands, verify no MySQL components remain:

# Check for remaining MySQL processes
ps aux | grep mysql

# Check for remaining MySQL files
sudo find / -name "*mysql*"

For systems with MariaDB or older MySQL versions:

# For systems that installed MariaDB instead
sudo apt-get purge mariadb-server mariadb-client

# For older MySQL 5.7 installations
sudo apt-get purge mysql-server-5.7 mysql-client-5.7

After complete removal, you can install fresh:

sudo apt-get update
sudo apt-get install mysql-server