<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="purge-binary-logs">
      </a>
      15.4.1.1 PURGE BINARY LOGS Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045177896800">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa11122073"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">PURGE</span> <span class="token datatype">BINARY</span> <span class="token keyword">LOGS</span> {
    <span class="token keyword">TO</span> <span class="token string">'<em class="replaceable">log_name</em>'</span>
  <span class="token operator">|</span> <span class="token keyword">BEFORE</span> <em class="replaceable">datetime_expr</em>
}</code></pre>
  </div>
  <p>
   The binary log is a set of files that contain information about
        data modifications made by the MySQL server. The log consists of
        a set of binary log files, plus an index file (see
   <a class="xref" href="binary-log.html" title="7.4.4 The Binary Log">
    Section 7.4.4, “The Binary Log”
   </a>
   ).
  </p>
  <p>
   The
   <a class="link" href="purge-binary-logs.html" title="15.4.1.1 PURGE BINARY LOGS Statement">
    <code class="literal">
     PURGE BINARY LOGS
    </code>
   </a>
   statement
        deletes all the binary log files listed in the log index file
        prior to the specified log file name or date. Deleted log files
        also are removed from the list recorded in the index file, so
        that the given log file becomes the first in the list.
  </p>
  <p>
   <a class="link" href="purge-binary-logs.html" title="15.4.1.1 PURGE BINARY LOGS Statement">
    <code class="literal">
     PURGE BINARY LOGS
    </code>
   </a>
   requires the
   <a class="link" href="privileges-provided.html#priv_binlog-admin">
    <code class="literal">
     BINLOG_ADMIN
    </code>
   </a>
   privilege. This
        statement has no effect if the server was not started with the
   <a class="link" href="replication-options-binary-log.html#option_mysqld_log-bin">
    <code class="option">
     --log-bin
    </code>
   </a>
   option to enable binary
        logging.
  </p>
  <p>
   Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa67158981"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">PURGE</span> <span class="token datatype">BINARY</span> <span class="token keyword">LOGS</span> <span class="token keyword">TO</span> <span class="token string">'mysql-bin.010'</span><span class="token punctuation">;</span>
<span class="token keyword">PURGE</span> <span class="token datatype">BINARY</span> <span class="token keyword">LOGS</span> <span class="token keyword">BEFORE</span> <span class="token string">'2019-04-02 22:46:26'</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The
   <code class="literal">
    BEFORE
   </code>
   variant's
   <em class="replaceable">
    <code>
     datetime_expr
    </code>
   </em>
   argument should
        evaluate to a
   <a class="link" href="datetime.html" title="13.2.2 The DATE, DATETIME, and TIMESTAMP Types">
    <code class="literal">
     DATETIME
    </code>
   </a>
   value (a
        value in
   <code class="literal">
    '
    <em class="replaceable">
     <code>
      YYYY-MM-DD
        hh:mm:ss
     </code>
    </em>
    '
   </code>
   format).
  </p>
  <p>
   <a class="link" href="purge-binary-logs.html" title="15.4.1.1 PURGE BINARY LOGS Statement">
    <code class="literal">
     PURGE BINARY LOGS
    </code>
   </a>
   is safe to run
        while replicas are replicating. You need not stop them. If you
        have an active replica that currently is reading one of the log
        files you are trying to delete, this statement does not delete
        the log file that is in use or any log files later than that
        one, but it deletes any earlier log files. A warning message is
        issued in this situation. However, if a replica is not connected
        and you happen to purge one of the log files it has yet to read,
        the replica cannot replicate after it reconnects.
  </p>
  <p>
   <a class="link" href="purge-binary-logs.html" title="15.4.1.1 PURGE BINARY LOGS Statement">
    <code class="literal">
     PURGE BINARY LOGS
    </code>
   </a>
   cannot be
        issued while a
   <a class="link" href="lock-instance-for-backup.html" title="15.3.5 LOCK INSTANCE FOR BACKUP and UNLOCK INSTANCE Statements">
    <code class="literal">
     LOCK INSTANCE FOR
        BACKUP
    </code>
   </a>
   statement is in effect for the instance,
        because it contravenes the rules of the backup lock by removing
        files from the server.
  </p>
  <p>
   To safely purge binary log files, follow this procedure:
  </p>
  <div class="orderedlist">
   <ol class="orderedlist" type="1">
    <li class="listitem">
     <p>
      On each replica, use
      <a class="link" href="show-replica-status.html" title="15.7.7.35 SHOW REPLICA STATUS Statement">
       <code class="literal">
        SHOW
            REPLICA STATUS
       </code>
      </a>
      to check which log file it is
            reading.
     </p>
    </li>
    <li class="listitem">
     <p>
      Obtain a listing of the binary log files on the source with
      <a class="link" href="show-binary-logs.html" title="15.7.7.2 SHOW BINARY LOGS Statement">
       <code class="literal">
        SHOW BINARY LOGS
       </code>
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Determine the earliest log file among all the replicas. This
            is the target file. If all the replicas are up to date, this
            is the last log file on the list.
     </p>
    </li>
    <li class="listitem">
     <p>
      Make a backup of all the log files you are about to delete.
            (This step is optional, but always advisable.)
     </p>
    </li>
    <li class="listitem">
     <p>
      Purge all log files up to but not including the target file.
     </p>
    </li>
   </ol>
  </div>
  <p>
   <code class="literal">
    PURGE BINARY LOGS TO
   </code>
   and
   <code class="literal">
    PURGE
        BINARY LOGS BEFORE
   </code>
   both fail with an error when binary
        log files listed in the
   <code class="filename">
    .index
   </code>
   file had
        been removed from the system by some other means (such as using
   <span class="command">
    <strong>
     rm
    </strong>
   </span>
   on Linux). (Bug #18199, Bug #18453) To
        handle such errors, edit the
   <code class="filename">
    .index
   </code>
   file
        (which is a simple text file) manually to ensure that it lists
        only the binary log files that are actually present, then run
        again the
   <a class="link" href="purge-binary-logs.html" title="15.4.1.1 PURGE BINARY LOGS Statement">
    <code class="literal">
     PURGE BINARY LOGS
    </code>
   </a>
   statement that failed.
  </p>
  <p>
   Binary log files are automatically removed after the server's
        binary log expiration period. Removal of the files can take
        place at startup and when the binary log is flushed. The default
        binary log expiration period is 30 days. You can specify an
        alternative expiration period using the
   <a class="link" href="replication-options-binary-log.html#sysvar_binlog_expire_logs_seconds">
    <code class="literal">
     binlog_expire_logs_seconds
    </code>
   </a>
   system variable. If you are using replication, you should
        specify an expiration period that is no lower than the maximum
        amount of time your replicas might lag behind the source.
  </p>
 </div>
 <br/>
</div>
