> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/sql-statements/administrative-sql-statements/purge-binary-logs.md).

# PURGE BINARY LOGS

Official PURGE BINARY LOGS syntax: delete binlogs using TO 'log\_name' or BEFORE datetime\_expr, replica read constraints, and SHOW BINARY LOGS commands.

## Syntax

```bnf
PURGE { BINARY | MASTER } LOGS
    { TO 'log_name' | BEFORE datetime_expr }
```

![Railroad diagram of PURGE BINARY LOGS — equivalent to the BNF above](https://2988006611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSsmexDFPv2xG2OTyO5yV%2Fuploads%2Fgit-blob-2df16b025fadc91deffb0eab39ed2280877e19ba%2Fpurge-binary-logs-railroad.svg?alt=media)

## Description

The `PURGE BINARY LOGS` statement deletes all the [binary log](/docs/server/server-management/server-monitoring-logs/binary-log.md) files listed in the log index file prior to the specified log file name or date. `BINARY` and `MASTER` are synonyms. Deleted log files are also removed from the list recorded in the index file, so that the given log file becomes the first in the list.

The datetime expression is in the format `YYYY-MM-DD hh:mm:ss`.

If a connected replica is still reading one of the binary log files that the statement would delete, the statement stops at that file. Older files are deleted, and that file and all later ones are kept. The statement does not fail. From [MariaDB 11.4.3](https://jira.mariadb.org/browse/MDEV-34504), it returns note 1375, which names the first file it kept, for example `Binary log 'mariadb-bin.000005' is not purged because it is in use by a slave thread`. Earlier releases stop without any message. Use [SHOW BINARY LOGS](/docs/server/reference/sql-statements/administrative-sql-statements/show/show-binary-logs.md) to check which files remain.

However, if the replica is not connected and has yet to read from a log file you delete, the file will be deleted, but the replica will be unable to continue replicating once it connects again.

From [MariaDB 11.4.3](https://jira.mariadb.org/browse/MDEV-34504), `PURGE BINARY LOGS` ignores [slave\_connections\_needed\_for\_purge](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#slave_connections_needed_for_purge), the minimum number of connected replicas that [automatic purging](/docs/server/server-management/server-monitoring-logs/binary-log/using-and-maintaining-the-binary-log.md#purging-log-files) requires. In MariaDB 11.4.1 and 11.4.2, `PURGE BINARY LOGS BEFORE` observed that limit while `PURGE BINARY LOGS TO` did not.

If an automatic purge has already been refused for a file because too few replicas had processed it, a manual purge of that same file is refused as well, reporting the file as the current active binary log even when it is not. This is a server bug, tracked as [MDEV-41214](https://jira.mariadb.org/browse/MDEV-41214); the misleading reason is tracked separately as [MDEV-41215](https://jira.mariadb.org/browse/MDEV-41215).

The server clears that state when a replica moves on to a new binary log file, or when [slave\_connections\_needed\_for\_purge](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#slave_connections_needed_for_purge) or [max\_binlog\_total\_size](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#max_binlog_total_size) is assigned a value, even the value it already has. Assigning either one does not clear it while [binlog\_expire\_logs\_seconds](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#binlog_expire_logs_seconds) is non-zero, because the assignment retries the automatic purge, and that retry is refused for the same file again. Set `binlog_expire_logs_seconds` to `0` first if you need to purge such a file manually.

This statement has no effect if the server was not started with the [--log-bin](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#log_bin) option to enable binary logging.

To list the binary log files on the server, use [SHOW BINARY LOGS](/docs/server/reference/sql-statements/administrative-sql-statements/show/show-binary-logs.md). To see which files they are reading, use [SHOW REPLICA STATUS](/docs/server/reference/sql-statements/administrative-sql-statements/show/show-replica-status.md). You can only delete the files that are older than the oldest file that is used by the slaves.

To delete all binary log files, use [RESET MASTER](/docs/server/reference/sql-statements/administrative-sql-statements/replication-statements/reset-master.md). To move to a new log file (for example if you want to remove the current log file), use [FLUSH LOGS](/docs/server/reference/sql-statements/administrative-sql-statements/flush-commands/flush.md) before you execute `PURGE LOGS`.

If the [expire\_logs\_days](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#expire_logs_days) server system variable is not set to 0, the server automatically deletes binary log files after the given number of days. From MariaDB 10.6, the [binlog\_expire\_logs\_seconds](/docs/server/ha-and-performance/standard-replication/replication-and-binary-log-system-variables.md#binlog_expire_logs_seconds) variable allows more precise control over binlog deletion, and takes precedence if both are non-zero.

Requires the [BINLOG ADMIN](/docs/server/reference/sql-statements/account-management-sql-statements/grant.md#binlog-admin) privilege.

## Examples

```sql
PURGE BINARY LOGS TO 'mariadb-bin.000063';
```

```sql
PURGE BINARY LOGS BEFORE '2013-04-21';
```

```sql
PURGE BINARY LOGS BEFORE '2013-04-22 09:55:22';
```

## See Also

* [Using and Maintaining the Binary Log](/docs/server/server-management/server-monitoring-logs/binary-log/using-and-maintaining-the-binary-log.md)
* [FLUSH LOGS](/docs/server/reference/sql-statements/administrative-sql-statements/flush-commands/flush.md).

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}
