How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Safely Clean Up MySQL Binary Log Files
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Safely Clean Up MySQL Binary Log Files
Server Management

How to Safely Clean Up MySQL Binary Log Files

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Safely remove MySQL binary log files to free disk space
SHARE

To safely free disk space taken by MySQL binary logs (mysql-bin.*), use the PURGE BINARY LOGS statement from inside MySQL — not rm from the shell. Purging through MySQL keeps the log index file consistent and is safe with replication. For ongoing cleanup, set binlog_expire_logs_seconds (or expire_logs_days on older versions) so MySQL drops old logs automatically.

Contents
  • Don’t rm them — use PURGE
  • Recommended — purge logs older than N days
  • Check replicas first if you have them
  • Auto-expire (recommended ongoing setting)
  • Verify and reclaim space
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on MySQL 8.0 and MariaDB 10.11. Originally published 2023-01-04, rewritten and updated 2026-05-17.

Don’t rm them — use PURGE

-- Drop everything older than a specific file
PURGE BINARY LOGS TO 'mysql-bin.001211';

-- Or older than a timestamp
PURGE BINARY LOGS BEFORE '2026-05-15 00:00:00';

PURGE BINARY LOGS updates both the on-disk files and the mysql-bin.index manifest. Hand-deleting files leaves the index pointing at things that don’t exist and causes MySQL to fail on next rotation.

MySQL binary logs — purge by file/date, automatic expiry, replica safety check

Recommended — purge logs older than N days

-- Three days
PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 3 DAY);

-- Seven days
PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 7 DAY);

Three to seven days is a sensible window for most non-replicated servers — enough to recover from “I did a stupid thing” without bloating the disk. If you’re not running replication, the only reason to keep binary logs is point-in-time recovery; pick a retention that matches your backup cadence.

Check replicas first if you have them

-- On each replica
SHOW SLAVE STATUS\G;
-- (MySQL 8.4+)
SHOW REPLICA STATUS\G;

Note the Master_Log_File (or Source_Log_File) on every replica. That’s the oldest file each replica is still reading. Never purge past it — purging past a replica’s current position breaks replication and forces a full re-sync.

Auto-expire (recommended ongoing setting)

-- MySQL 8.0+ — seconds
SET GLOBAL binlog_expire_logs_seconds = 259200;   -- 3 days

-- MySQL 5.7 / MariaDB — days
SET GLOBAL expire_logs_days = 3;

Make it persistent by adding the same setting to /etc/my.cnf (or the appropriate file in /etc/my.cnf.d/ / /etc/mysql/conf.d/):

[mysqld]
# MySQL 8.0+
binlog_expire_logs_seconds = 259200

# MySQL 5.7 / MariaDB
expire_logs_days = 3

MySQL rotates and removes logs older than the limit during normal operation — no manual purges needed.

Verify and reclaim space

-- Confirm what's left
SHOW BINARY LOGS;

-- At the OS
du -sh /var/lib/mysql/mysql-bin.*

Frequently asked questions

Why is deleting mysql-bin.* files with rm dangerous?

MySQL also maintains mysql-bin.index, a manifest of which binary log files exist. rm-ing the files leaves the index pointing at non-existent files. On the next start, MySQL refuses to rotate or crashes trying to read them. PURGE BINARY LOGS updates both the files and the index atomically — that’s why it’s the supported path.

How do I know if a replica still needs the older logs?

Run SHOW SLAVE STATUS\G (or SHOW REPLICA STATUS\G on MySQL 8.4+) on every replica and note Master_Log_File / Source_Log_File. That’s the oldest file any replica is still reading. Never purge past it — replicas need it to continue. PURGE BINARY LOGS TO 'file-just-after' stops at the safe boundary.

Why did expire_logs_days get renamed?

MySQL 8.0 replaced expire_logs_days with binlog_expire_logs_seconds — same idea, finer granularity. Default is 2,592,000 seconds (30 days). On MariaDB and MySQL 5.7, expire_logs_days still works. Use the variable that matches your server version; the older name produces a deprecation warning on MySQL 8.0+.

What if MySQL won’t start because the disk is full?

Free space outside the data directory first — clear /tmp, old logs in /var/log, package caches. Once MySQL has even a few MB of headroom it can start. Then connect and PURGE BINARY LOGS BEFORE NOW() - INTERVAL 1 DAY to free real space. If you absolutely cannot start MySQL, edit my.cnf to add skip-log-bin, start, take a backup, then re-enable logging and purge properly.

Related guides

  • How to Find What’s Pinning MySQL at 100% CPU
  • How to Troubleshoot MariaDB Not Starting
  • How to Check Which MySQL Database Is Using the Most CPU

References

MySQL PURGE BINARY LOGS: dev.mysql.com/doc/refman/8.0/en/purge-binary-logs.html. binlog_expire_logs_seconds: dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_binlog_expire_logs_seconds. MariaDB binary log overview: mariadb.com/kb/en/binary-log.

TAGGED:configurationmariadbmysqlperformance

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Completely remove MariaDB from a RHEL-family server How to Remove MariaDB Completely from RHEL/CentOS
Next Article Remove unwanted characters from a PHP string with regex How to Remove Unwanted Characters from a String in PHP
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run a Linux cron job as a non-root user
How to Run a Cron Job as a Non-Root User
May 22, 2026
Reset the LiteSpeed WebAdmin Console password
How to Reset the LiteSpeed WebAdmin Console Password
May 22, 2026
Replace strings in a MySQL database with UPDATE and REPLACE()
How to Replace Strings in a MySQL Database
May 22, 2026
Rename menu items on the WooCommerce My Account page
How to Rename Menu Items on the WooCommerce My Account Page
May 22, 2026
Remove unwanted characters from a PHP string with regex
How to Remove Unwanted Characters from a String in PHP
May 22, 2026

You Might Also Like

Installing Docker on AlmaLinux guide
Server Management

Install Docker on AlmaLinux: Step-by-Step (Docker CE + Compose)

4 Min Read
Installed Discourse on AlmaLinux
Server Management

How I Installed Discourse on AlmaLinux (Docker Method, Step-by-Step)

6 Min Read
MySQL top CPU usage — PROCESSLIST snapshot and performance_schema digest
Server Management

How to Check Which MySQL Database or User Is Using the Most CPU

8 Min Read
Laravel migration adding two new columns to an existing transactions table
Web Development

How to Add New Columns to an Existing Table in Laravel Migration

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?