In this article, I'll show you how to completely remove MySQL (or MariaDB) from Ubuntu 24.04, whether you're facing storage issues, have forgotten your password, or need to reinstall it.

All the steps mentioned are recommended ones, and each step is explained in detail in its respective section. So, let's begin…

How to Uninstall MySQL from Ubuntu 24.04

If you have access to the MySQL console, it's always advisable to take a backup of your database before beginning any of the steps involved in MySQL uninstallation.

Step 1: Take MySQL Database Backup

The first step is to enter the MySQL console and execute the following command to list all MySQL databases.

MySQL> show databases;
listing all mysql databases

Take note of the MySQL database you wish to export, then exit the MySQL console and use the following command, specifying the MySQL username, password, and database for exporting in SQL format.

$ sudo mysqldump -u [user] -p [db_name] > [export_the_db.sql]
exporting mysql database

Once you have exported the MySQL database, you can then proceed to fully remove MySQL from your Ubuntu system.

Step 2: Stop MySQL Service

First, make sure to stop the MySQL service by running:

$ sudo systemctl stop mysql
stopping mysql service

Step 3: Remove MySQL Packages

Then remove all MySQL-related packages.

$ sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

During removal, it may ask you to approve the removal of all databases; select "Yes" to proceed.

removing MySQL packages

Step 4: Delete MySQL-Related Files

Now that we have completely removed all MySQL packages, make sure to delete the remaining MySQL related files and data by running:

$ sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql
removing MySQL related files

Step 5: Clean Unneeded Packages

During MySQL installation, some dependencies are installed along with it that are no longer needed after its removal, so you can remove them by running:

$ sudo apt autoremove && sudo apt autoclean
removing MySQL dependencies

Step 6: Remove MySQL Configurations

Finally, make sure to remove all the remaining MySQL configuration by running:

$ sudo apt remove dbconfig-mysql
removing MySQL configuration

That's all it takes to completely get rid of MySQL from Ubuntu 24.04. If you are facing any difficulties, feel free to reach out to us via comment.