How to PURGE binary logs in MySQL & MariaDB
The binary log is a set of log files that contain information about data modifications made by the MySQL server instance. You can see there are multiple binary files will be available on your MySQL server and there will be one file named .index which contains names of all binary files to keep track of them.
To PURGE binary logs, you can follow below steps.
Step 1. List Binary Files
First, you will need to list the binary log files in your system and find out how old binary log files you want to delete. The binary log files generally located under /var/lib/mysql directory.
# ls -a /var/lib/mysql
...
-rw-rw---- 1 mysql mysql 3800220 Mar 20 15:15 mysql-bin.000745
-rw-rw---- 1 mysql mysql 1076727 Mar 20 15:40 mysql-bin.000746
-rw-rw---- 1 mysql mysql 263024 Mar 20 15:42 mysql-bin.000747
-rw-rw---- 1 mysql mysql 13895153 Mar 25 00:36 mysql-bin.000748
-rw-rw---- 1 mysql mysql 2717571 Mar 25 01:02 mysql-bin.000749
-rw-rw---- 1 mysql mysql 4080285 Mar 25 03:49 mysql-bin.000750
...
Step 2. PURGE Binary Logs
From MySQL prompt, you can use any of the below commands to delete older binary log files as per your system requirements.
- To delete binary log file mysql-bin.000745 or older files
mysql> PURGE BINARY LOGS TO 'mysql-bin.000745';
- To delete all binary log files created till date “2021-03-20 23:59:59”.
mysql> PURGE BINARY LOGS BEFORE '2021-03-20 23:59:59';
You can list binary log files now. You will find that all the old logs has been removed from mysql directory.
That’s it 🙂