Do you know what is Binary Log in MySQL? Binary logs are transactional logs in MySQL. Whatever changes are made to the database, that is stored in the Binary Logs. In our previous article, we have seen how to enable/disable binary logs in MySQL. Also, we can purge these as well based on date, time, sequence, etc. Today in this article, we will see how to purge binary logs in MySQL.
As the number of Binary Logs is increasing, its better to clear them after regular backup. Hence its important to Purge Binary Logs in MySQL. Also, it can save space as well.
1. Purge Binary Logs based on Sequence :
mysql> purge binary logs to 'binlog.000004';

2. Purge Binary Logs based on Date and Time :
We can find the binlogs datewise in the Binlogs directory. By default it we be in data directory. We can find the location from below command :
mysql> show variables like ‘datadir’;
+—————+—————–+
| Variable_name | Value |
+—————+—————–+
| datadir | /var/lib/mysql/ |
+—————+—————–+
1 row in set (0.15 sec)
If we go to the location, can see datewise binlogs as below :

Suppose, we want to purge binary keeping all the today’s (Jan2) log. Then, please issue the command :
Syntax : PURGE BINARY LOGS BEFORE 'YYYY-MM-DD HH:MM:SS'; Example : purge binary logs before '2023-12-30 00:00:00';

Hope this article will help to understand how to purge binary logs in MySQL . Also, you can follow the MySQL Doc for the same.
Pingback: How to Enable/Disable Binlogs in MySQL :
Pingback: How to check Audit logs in MySQL :