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
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Fix XAMPP MySQL shutdown unexpectedly error
Server Management

How to Fix “Error: MySQL Shutdown Unexpectedly” in XAMPP

5 Min Read
Laravel call controller from another controller — app() and constructor injection patterns
Web Development

How to Call a Controller Method from Another Controller in Laravel

8 Min Read
Linux add and delete users from the terminal — adduser, passwd, sudo group, userdel
Server Management

How to Add and Delete Users on a Linux Server from the Terminal

7 Min Read
JavaScript format number with decimals — toFixed, Math.floor, and Intl.NumberFormat
Web Development

How to Format a Number with Decimals in JavaScript

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?