HomeMySQLHow to clear or purge MySQL Bin log Files

How to clear or purge MySQL Bin log Files

Binary log

MySQL’s Official website defines the binary log as follows:

The binary log contains “events” that describe database changes such as table creation operations or changes to table data. It also contains events for statements that potentially could have made changes (for example, a DELETE that matched no rows). The binary log also contains information about how long each statement took that update data. The binary log has two important purposes:

a) Data replication

b) Also certain data recovery options require this binary log

Considering the replication purpose, it is necessary to clear the old logs for the proper functioning of replication. Also clearing the old logs will free up a large about of disk space.

Here is some example commands to clear/purge MySQL binary logs.

Access the MySQL prompt.

The following command will delete all logs prior to the log file mysql-bin.0003

mysql> PURGE BINARY LOGS TO 'mysql-bin.0003';

The following command will delete all logs before 2015-09-15

mysql> PURGE BINARY LOGS BEFORE '2015-09-15 10:00:00';

The following command will delete all logs older than 7 days.

mysql> PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);

The following command will delete all logs till now.

mysql> PURGE BINARY LOGS BEFORE now();

You will not need to do purge logs manually, logs older than 10 days will be purged automatically by the MySQL server if you add the following lines in the MySQL configuration file.

expire_logs_days = 10
max_binlog_size = 100M

Restart mysql server

/etc/init.d/mysql restart

Done!

Scroll to Top