PURGE {MASTER | BINARY} LOGS TO 'log_name'
PURGE {MASTER | BINARY} LOGS BEFORE 'date'
Deletes all the binary logs listed in the log index prior to the specified log or date. The logs also are removed from the list recorded in the log index file, so that the given log becomes the first.
This statement has no effect if the --log-bin
option has not been enabled.
Example:
PURGE MASTER LOGS TO 'mysql-bin.010'; PURGE MASTER LOGS BEFORE '2003-04-02 22:46:26';
The BEFORE variant's
date argument can be in
'YYYY-MM-DD hh:mm:ss' format.
MASTER and BINARY are
synonyms.
This statement is safe to run while slaves are replicating. You do not need to stop them. If you have an active slave that currently is reading one of the logs you are trying to delete, this statement does nothing and fails with an error. However, if a slave is dormant and you happen to purge one of the logs it has yet to read, the slave will be unable to replicate after it comes up.
To safely purge logs, follow this procedure:
On each slave server, use SHOW SLAVE
STATUS to check which log it is reading.
Obtain a listing of the binary logs on the master server
with SHOW BINARY LOGS.
Determine the earliest log among all the slaves. This is the target log. If all the slaves are up to date, this is the last log on the list.
Make a backup of all the logs you are about to delete. (This step is optional, but always advisable.)
Purge all logs up to but not including the target log.
You can also set the expire_logs_days system
variable to expire binary log files automatically after a given
number of days (see Section 5.1.3, “System Variables”).
If you are using replication, you should set the variable no
lower than the maximum number of days your slaves might lag
behind the master.
Prior to MySQL 5.0.60, PURGE BINARY LOGS TO
and PURGE BINARY LOGS BEFORE did not behave
in the same way (and neither one behaved correctly) when binary
log files listed in the .index file had
been removed from the system by some other means (such as using
rm on Linux). Beginning with MySQL 5.0.60, both variants of the
statement fail with an error in such cases. (Bug#18199, Bug#18453) You can handle such errors by editing the
.index file (which is a simple text file)
manually and insuring that it lists only the binlog files that
are actually present, then running again the PURGE
BINARY LOGS statement that failed.

User Comments
A nice trick to have a moving window of master logs -- e.g. so you can set up a cron job to trim them -- is to run a query like:
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 31 DAY);
So you could set up a weekly cron job like:
0 9 * * mon mysql -uroot -e "PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 31 DAY);"
Heres a good way to purge the logs if you only have one slave (run this on the slave):
mysql -uroot -hMaster_HostName -pMasterPassword -e "PURGE MASTER LOGS TO '`mysql -uroot -pSlavePassword -e 'show slave status\G'|awk '$1 == "Master_Log_File:" {print $2}'`';"
What it does is it checks to see which binary log the slave is currently reading and then connects to the master and purges all binary logs up to, but not including, that log.
Another variant of purging log files(run this on slave):
mysql -pMASTER_PWD -e "PURGE MASTER LOGS TO '`mysql -h SLAVE_HOST -pSLAVE_PWD -e 'SHOW SLAVE STATUS\G'|grep -m 1 Master_Log_File|sed "s/^.*: //g"`'"
You can also set "expire_log_days" to a value such as 7 for automatic binlog purge.
The variable to set in the previous comment is actually called "expire_logs_days" (with logs plural).
Just to save you hunting for docs like I did!
Is expire_logs_days or PURGE MASTER LOGS not working for you? It might be because you've deleted a file manually, and your [host]-bin.index file still thinks it exists. You can find out by either comparing the contents of [host]-bin.index to what files exist, or by running SHOW MASTER LOGS and looking for files with a size of zero.
Correcting [host]-bin.index so it matches what's on the filesystem fixes this problem for me.
IF you delete the binary logs from command line, MySQL stops logging until you RESET MASTER. I think this is safer than editing the mysql-bin.index directly as posted above.
Add your own comment.