When the need for MySQL on an Ubuntu server goes away, users often reach for the apt remove command. But did you know that this command removes the program files yet can leave the configuration behind?
To avoid this and remove the packages along with the system configs in one go, you should use the apt purge command instead. The MySQL data directory needs to be cleaned up separately. Before removing /var/lib/mysql, be sure to check the database — there might be important data in there. Better yet, make a backup first.
remove vs purge — what's the difference
The first thing to understand and remember is that removing a package is not the same as deleting the database itself. On Ubuntu, the mysql-server package is a metapackage. It, in turn, depends on the current server version. For reference — Ubuntu 22.04 and 24.04 LTS both carry mysql-server-8.0 in their repositories.
So apt essentially offers two main ways to remove packages:
Command | What happens |
sudo apt remove mysql-server | The package and program files are removed, but configuration files belonging to the package are often left behind |
sudo apt purge mysql-server | The package is removed from the server along with its system configuration files |
It's a mistake to think purge wipes out literally everything. That's not the case. The command doesn't erase absolutely everything related to MySQL. For example, /var/lib/mysql data and some directories created after installation or modified by the user remain.
Step-by-step guide to removing MySQL Server
To remove the MySQL server from Ubuntu, you need to perform a few simple steps.
Check what's actually installed on the OS
Before removing anything, it's useful to check which MySQL packages are present:
dpkg -l | grep -E 'mysql|mariadb'When the server is installed via the standard Ubuntu packages, the result is usually mysql-server, mysql-server-8.0, and mysql-server-core-8.0. These packages ship as part of Ubuntu 22.04/24.04.
Stopping MySQL
First, stop the service:
sudo systemctl stop mysqlCheck its status with:
sudo systemctl status mysqlIf the service is already stopped, that's fine.
Removing server packages with apt purge
For a standard MySQL 8 installation
sudo apt purge mysql-server mysql-server-8.0 mysql-server-core-8.0If one of the packages isn't present on the system, apt will report that. This doesn't mean the rest of the packages won't be removed.
Avoid running the following command on a production server without checking first:
sudo apt purge 'mysql-*'It often affects client libraries, tools, and other MySQL packages that installed applications may still need. If you really need to remove every MySQL component, first review the full list with dpkg -l, then remove the unneeded packages selectively.
Cleaning up unneeded dependencies
After removing the server, run:
sudo apt autoremove --purgeBefore confirming the operation, carefully review the list of packages involved. What apt is about to remove shouldn't come as a surprise. If the list includes software needed by another application on the server, simply cancel the operation.
Removing leftover configs and data
After apt purge, check the MySQL directory:
sudo ls -la /etc/mysqlAs well as the data directory:
sudo ls -la /var/lib/mysqlIt's important to keep in mind that /etc/mysql holds the MySQL configuration; the mysql-common package, for example, includes the shared configuration file /etc/mysql/my.cnf. If the goal is to remove MySQL entirely, including its configuration, delete the remaining directory after checking it:
sudo rm -rf /etc/mysqlBut the data directory is a fundamentally different story. /var/lib/mysql is the most critical thing to check — this is where MySQL's databases and internal files live. A command like the following should never be run automatically:
sudo rm -rf /var/lib/mysqlMake a backup if the server held any production databases. For instance, you can copy the directory to a separate disk or take a database dump. Once you're confident the data is no longer needed:
sudo rm -rf /var/lib/mysqlThere may also be a directory at:
sudo ls -la /var/log/mysqlIf it's still there and MySQL is no longer in use:
sudo rm -rf /var/log/mysqlWe don't recommend deleting the mysql system user just to wipe out every last trace of MySQL from the OS. If MariaDB or other software relying on that same user is installed on the server, manually removing the account can create new problems down the line.
So before deciding to run the command above, make sure the user isn't needed anywhere else:
getent passwd mysql
getent group mysqlHow to verify MySQL has been fully removed
After cleanup, check the service:
systemctl status mysqlIf MySQL has been removed, the service shouldn't be active.
Check for running processes:
ps aux | grep '[m]ysqld'If the command returns nothing, the mysqld process isn't running.
Check installed packages:
dpkg -l | grep -i mysqlSome client libraries or other components may still be listed here. Their presence alone doesn't mean the MySQL server is still running.
Check the main directories:
sudo ls -ld /etc/mysql /var/lib/mysql /var/log/mysqlIf the directories were removed, ls will report that they don't exist.
Finally, you can check whether anything is listening on the default MySQL port:
sudo ss -lntp | grep ':3306'An empty result means nothing is currently listening on TCP port 3306.
Common issues when removing MySQL
Now you know how to remove MySQL from Ubuntu, but this seemingly simple task often runs into a few snags along the way. Let's look at the most common situations you're likely to encounter in practice.
dpkg reports unfinished package configuration
If a previous package operation was interrupted, first try:
sudo dpkg --configure -aThen:
sudo apt --fix-broken installOnce the package manager's state has been restored, run apt purge again.
apt reports dependency issues
Don't remove dependencies manually with rm. Check which packages are blocking the operation:
apt depends mysql-serverand the list of installed components:
dpkg -l | grep -E 'mysql|mariadb'Then remove the unneeded packages through apt, not by hand.
/var/lib/mysql remains after purge
This isn't necessarily a mistake. Removing the package and removing user data are two separate operations. If the data is no longer needed, you can delete the directory manually after taking a backup.
The application stopped working after removing MySQL
Check the application's configuration. It may have been using MySQL as its primary database. Removing the server software doesn't automatically migrate data to another DBMS.
What's next — reinstalling or switching to another engine
If MySQL was removed because of a broken installation, reinstalling the server is usually enough:
sudo apt update
sudo apt install mysql-serverUbuntu treats the mysql-server package as a metapackage that installs the current server version from the distribution's repository. On current Ubuntu 22.04 and 24.04 LTS releases, that's the MySQL 8.0 branch.
If the goal is to replace MySQL, MariaDB or PostgreSQL are worth considering. MariaDB is compatible with many applications built for MySQL, but the switch should still be validated at the level of the specific application, SQL queries, drivers, and database structure.
On a VPS, this kind of migration is especially convenient: with root access, the administrator has full control over packages, configuration, the file system, and backups. If your next project needs a dedicated server, you can choose a virtual server from Leneex and set up the environment yourself.







