How to PURGE binary logs in MySQL & MariaDB

If you found this post helpful, kindly share it and show your support :)

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 🙂


Mohammed Noufal

I'm Mohammed Noufal, working as Server Admin for the last 10 years.  In my day-to-day life, l had to face different problems related to Web-hosting. On my website, you can find solutions for different Web-hosting-related problems. Sometimes, I spent hours searching/googling to find a solution. This inspired me to start a website that gives solutions to different Webhosting problems. This website includes basic Linux and windows commands, and different control panels like cPanel, Plesk, DirectAdmin, Webmin & so on. You can find solutions and or suggestions for different Web-hosting related problems from here. Hence I would like to say Errorlogz is your server protector.  I will be glad if Logdetect can help any one of the Server admins to find a solution to his problem.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *