Skip to content

MySQL binary logs filling the disk: purge them safely

Binary logs record every change to the database and are kept until an expiry setting removes them. If /var/lib/mysql keeps growing, check SHOW BINARY LOGS, remove old files with PURGE BINARY LOGS after checking replicas and backups, and set binlog_expire_logs_seconds to a retention you actually need. Never delete binlog files with rm: the server's index file would no longer match the files on disk.

If the disk of a database server fills up and /var/lib/mysql keeps growing even though the data hardly changes, the binary logs are the usual suspect. They record every write to the database and are only removed when an expiry setting or an administrator does it. The safe fix is three steps: check which files exist and who still needs them, remove old ones with PURGE BINARY LOGS, and set a retention that matches your replication and backup needs. Do not delete the files with rm.

The commands work on MySQL 8 and MariaDB; differences are noted. On current Debian and Ubuntu with MariaDB the client is called mariadb; mysql usually works as well.

How do I know the binary logs are the problem?

Start on the shell. If the disk is full, the general approach in Linux disk full: find what is using the space leads you to the data directory. There, look at the binlog files:

du -sh /var/lib/mysql
ls -lh /var/lib/mysql | grep -E 'binlog|bin\.[0-9]'
38G	/var/lib/mysql
-rw-r----- 1 mysql mysql 1.1G Nov 19 22:40 binlog.000412
-rw-r----- 1 mysql mysql 1.1G Nov 20 03:17 binlog.000413
-rw-r----- 1 mysql mysql 412M Nov 20 08:02 binlog.000414
-rw-r----- 1 mysql mysql  208 Nov 20 08:02 binlog.index

MySQL 8 names the files binlog.000001 and so on when log_bin is not set to a path. Many older configurations, and MariaDB on Debian when you uncomment the line prepared in its configuration, use log_bin = /var/log/mysql/mysql-bin.log, so the files sit in /var/log/mysql. Ask the server where they are and whether logging is on at all:

SHOW VARIABLES LIKE 'log_bin%';
SHOW BINARY LOGS;
Variable_name    Value
log_bin          ON
log_bin_basename /var/lib/mysql/binlog
log_bin_index    /var/lib/mysql/binlog.index
log_bin_trust_function_creators OFF

Log_name       File_size   Encrypted
binlog.000381  1073742011  No
binlog.000382  1073741970  No
...
binlog.000414  431908112   No

MariaDB shows only Log_name and File_size. If log_bin is OFF, the binary logs are not your problem.

Which settings decide how much space they take?

Two values matter: how long files are kept and how large one file may grow.

-- MySQL 8 and MariaDB 10.6 or later
SHOW VARIABLES LIKE 'binlog_expire_logs_seconds';
-- MariaDB, all versions
SHOW VARIABLES LIKE 'expire_logs_days';
SHOW VARIABLES LIKE 'max_binlog_size';
Variable_name               Value
binlog_expire_logs_seconds  2592000

Variable_name    Value
max_binlog_size  1073741824
  • MySQL 8.0 and later keep binary logs for 30 days by default (2592000 seconds). expire_logs_days is deprecated in 8.0 and gone in 8.4.
  • MariaDB uses expire_logs_days; from 10.6 it also accepts binlog_expire_logs_seconds, and the two describe the same setting (setting one changes the other). The built-in default is 0, which means the files are never removed automatically; the MariaDB packages for Debian and Ubuntu set expire_logs_days = 10 in 50-server.cnf.
  • max_binlog_size only sets when the server starts a new file (default 1 GB). It does not limit the total.

The total is roughly your write volume per day times the retention. A shop that writes 15 GB of binlogs a day keeps 450 GB with the 30-day default, which is often more than the disk has.

Expiry runs when the server starts and when it switches to a new binlog file. It is not a background job: a lowered value takes effect at the next rotation, which you can force with FLUSH BINARY LOGS.

Who still needs the old files?

Before you remove anything, answer two questions.

Do replicas read from this server? A replica fetches the binary logs of its source. If you purge a file a replica has not read yet, replication stops and the replica has to be rebuilt. On each replica, run:

SHOW REPLICA STATUS\G      -- MySQL 8.0.22 or later, MariaDB 10.5.1 or later
SHOW SLAVE STATUS\G        -- older versions; removed in MySQL 8.4, still accepted by MariaDB

An excerpt of the output on MySQL 8:

              Source_Log_File: binlog.000414
          Read_Source_Log_Pos: 431907640
        Relay_Source_Log_File: binlog.000413
           Replica_IO_Running: Yes
          Replica_SQL_Running: Yes
        Seconds_Behind_Source: 0

Older MySQL versions and MariaDB (also with SHOW REPLICA STATUS) call the fields Master_Log_File, Relay_Master_Log_File and Slave_IO_Running. Keep at least the oldest of these files across all replicas, and everything after it. Watch out for replicas that are stopped or far behind; they are the ones that break.

Do your backups rely on binary logs? For point-in-time recovery you restore the last full backup and then replay the binary logs written since then with mysqlbinlog, up to the moment before the mistake. Without those files, you can only return to the last full backup. Keep at least the binlogs since your oldest backup that you would still restore from. Whether those backups exist and are recent is a separate question.

How do I remove old binary logs safely?

Use PURGE BINARY LOGS. It deletes the files and updates the index file in one step. There are two forms:

-- everything before this file (the file itself stays)
PURGE BINARY LOGS TO 'binlog.000400';

-- everything older than a point in time
PURGE BINARY LOGS BEFORE '2026-11-17 00:00:00';
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 3 DAY;

Afterwards, SHOW BINARY LOGS lists fewer files, and df -h shows the space back. The statement needs the BINLOG_ADMIN privilege in MySQL 8 (or SUPER); on a default Debian or Ubuntu installation the root account over the local socket has it.

On a source, MySQL will not delete a file that a connected replica is reading at that moment. That does not protect replicas that are disconnected, so the check above still matters.

Warning: Do not remove binlog files with rm, find -delete or a cleanup script. The index file still lists them; the server then reports errors when purging or rotating, replicas and backup tools look for files that are not there, and recovery gets harder. For the same reason, avoid RESET MASTER (in 8.4: RESET BINARY LOGS AND GTIDS) on a server with replicas: it deletes every binary log and starts numbering again.

How do I set a sensible retention?

Choose a retention that covers your longest replica outage and the time since your oldest usable backup, plus a margin. Seven days is a common value. Set it at runtime so it applies at once:

SET GLOBAL binlog_expire_logs_seconds = 604800;   -- MySQL 8, MariaDB 10.6+
SET GLOBAL expire_logs_days = 7;                  -- older MariaDB
FLUSH BINARY LOGS;

Runtime values are lost on restart. Make it permanent in the configuration (/etc/mysql/mysql.conf.d/mysqld.cnf for MySQL on Debian and Ubuntu, /etc/mysql/mariadb.conf.d/50-server.cnf for MariaDB, a file under /etc/my.cnf.d/ on the RHEL family):

[mysqld]
binlog_expire_logs_seconds = 604800

On MariaDB older than 10.6 write expire_logs_days = 7 instead. MySQL 8 can also store the runtime change with SET PERSIST.

Can I switch binary logs off?

If the server is not a replication source and your backups do not use binlogs, yes. In MySQL 8 add one line and restart:

[mysqld]
skip-log-bin

disable-log-bin is an equivalent spelling. On MariaDB binary logging is off unless log_bin is set, so remove or comment out that line. Check afterwards with SHOW VARIABLES LIKE 'log_bin'. Remember that switching it off also ends point-in-time recovery.

What happens when the disk is already full?

When MySQL cannot write because the disk is full, it usually waits rather than fails: it writes a message like this to the error log and retries every minute (MySQL 8.4, file name shortened):

2026-11-20T03:17:54.990516Z 13 [ERROR] [MY-000035] [Server] Disk is full writing '/var/lib/mysql/binlog.000414' (OS errno 28 - No space left on device). Waiting for someone to free space... Retry in 60 secs. Message reprinted in 600 secs.

For the application this looks like a hang: queries pile up, connections fill up, and you may see Too many connections. In MySQL, a failure to write the binary log can also stop the server, because binlog_error_action is ABORT_SERVER by default.

To recover, free some space on the same file system first, for example old compressed logs or a forgotten dump. Do not count on PURGE BINARY LOGS at this moment: while the server waits for space it holds the lock on the binary log, and statements that need it – SHOW BINARY LOGS and PURGE BINARY LOGS among them – can hang as well. Once there is space again, the server continues at its next retry, and then you purge as described above.

Common mistakes

  • Deleting binlogs with rm. The space comes back, the trouble starts later.
  • Purging without asking the replicas. A replica that was down for maintenance cannot catch up afterwards.
  • Relying on max_binlog_size. It sets the size of each file, not the total.
  • Changing the retention only at runtime. After the next restart the old value is back.
  • Expiry set to 0 on MariaDB. The files are kept forever.

Checklist

  1. Check with SHOW VARIABLES LIKE 'log_bin%' whether binary logging is on and where the files are.
  2. List the files with SHOW BINARY LOGS and compare their size with free space.
  3. Read the retention: binlog_expire_logs_seconds or expire_logs_days.
  4. On every replica, note the oldest file still needed.
  5. Check which binlogs your point-in-time recovery needs.
  6. Purge with PURGE BINARY LOGS TO or BEFORE, never with rm.
  7. Set the retention at runtime and in the configuration file.

How Lynx Heartbeat helps

The Lynx Heartbeat agent records the fill level of every file system every minute, and Lynx reads the trend: when a disk will be full in less than seven days, you get a warning such as "disk full in ≈ 9 days", while there is still time to act during working hours. The agent also reads MySQL status counters such as connections and slow queries, through an account that sees counters and never table contents. Lynx does not purge binary logs or change your database configuration; deciding which files can go stays with you. How the forecast works is described on the page Server monitoring.

Frequently asked questions

Can I delete MySQL binlog files with rm?

No. The server keeps a list of its binary logs in an index file. Files removed with rm stay in that list, which leads to errors on PURGE, in replication and in backup tools. Use PURGE BINARY LOGS TO or PURGE BINARY LOGS BEFORE, which removes the files and updates the index together.

How long does MySQL keep binary logs by default?

MySQL 8.0 and later enable binary logging by default and keep the files for 30 days (binlog_expire_logs_seconds = 2592000). MariaDB has binary logging switched off unless log_bin is set; when it is on, the retention comes from expire_logs_days or, from MariaDB 10.6, binlog_expire_logs_seconds. The built-in default is 0 (keep forever); the Debian and Ubuntu packages set 10 days.

Is it safe to purge binary logs on a replication source?

Only for files that every replica has already read. Check SHOW REPLICA STATUS on each replica and keep at least the file in Source_Log_File and everything after it. A replica that is stopped or far behind breaks if the files it still needs are gone.

Do I need binary logs if I do not use replication?

Possibly, for point-in-time recovery: a full backup plus the binary logs written since then lets you restore to a moment just before a mistake. If you neither replicate nor use binlogs for recovery, you can switch binary logging off with skip-log-bin in MySQL 8 or by not setting log_bin in MariaDB.

Servers

Related articles

All articles