Sometimes you need to completely remove MySQL from Ubuntu — maybe you’re switching to PostgreSQL or MariaDB, fixing a broken installation, or cleaning up a server. A standard apt remove leaves behind config files and databases. This guide shows you how to purge everything so there are no traces of MySQL left on the system.
These commands work on Ubuntu 22.04 and 24.04, including EC2 instances and WSL2.
Back Up Your Data First
Purging MySQL permanently deletes all databases, users, and configuration. If you have data you need to keep, export it before continuing:
sudo mysqldump --all-databases > mysql-backup.sql
For automated backups to S3, see How to Automate MySQL Database Backups on EC2 to Amazon S3.
Step 1: Stop the MySQL Service
Stop MySQL so no processes are using the files you’re about to remove:
sudo systemctl stop mysql
Step 2: Purge MySQL Packages
Use apt purge to remove MySQL packages along with their configuration files. A regular apt remove leaves config files behind — purge deletes everything.
sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
Step 3: Remove Data and Config Directories
The purge command removes packages but may leave behind the data directory and config files. Delete them manually:
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql
/etc/mysql— configuration files/var/lib/mysql— all databases and table data/var/log/mysql— log files
Step 4: Remove Orphaned Dependencies
Clean up packages that were installed as MySQL dependencies and are no longer needed:
sudo apt autoremove -y
Step 5: Verify the Removal
Check that no MySQL packages remain on the system:
dpkg -l | grep mysql
If this returns no output, MySQL has been fully removed. If you still see packages listed, run the purge command again targeting those specific package names.
You can also confirm the service is gone:
sudo systemctl status mysql
This should return Unit mysql.service could not be found.
Conclusion
MySQL is now fully removed from your system, including all databases, config files, and logs. If you want to reinstall it later, see How to Install MySQL Database on EC2 Ubuntu 22.04 LTS. If you’re switching databases, check out How to Install PostgreSQL on WSL Ubuntu 22.04.