Common SQL Errors

MySQL Error 1840: ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL error 1840 occurs when SET GLOBAL GTID_PURGED is executed while GTID_EXECUTED already contains transactions.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1840?

<p>MySQL Error 1840: ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY means GTID_PURGED can be set only on a server whose GTID_EXECUTED set is empty. Fix it by running RESET MASTER or by provisioning a fresh instance before issuing SET GLOBAL GTID_PURGED.</p>

Error Highlights

Typical Error Message

@@GLOBAL.GTID_PURGED can only be set when

Error Type

Replication Configuration Error

Language

MySQL

Symbol

ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY

Error Code

1840

SQL State

HY000

Explanation

Table of Contents

What is MySQL Error 1840: ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY?

Error 1840 fires when you run SET GLOBAL GTID_PURGED on a server that already has transactions in @@GLOBAL.GTID_EXECUTED. MySQL blocks the statement to protect GTID consistency.

The error typically appears during replication setup, point-in-time recovery, or after importing a dump created with --set-gtid-purged=ON. Clearing GTID_EXECUTED or starting from a clean server resolves the conflict.

What Causes This Error?

The main trigger is a non-empty GTID_EXECUTED set. MySQL allows GTID_PURGED to be set only when the server has executed no GTID transactions, ensuring that purged GTIDs align with real history.

Attempting to replay a dump containing SET GLOBAL GTID_PURGED statements on an active server, or reinitializing replication without cleaning metadata, will also raise the error.

How to Fix MySQL Error 1840

Make GTID_EXECUTED empty, then reissue SET GLOBAL GTID_PURGED. The quickest method is RESET MASTER; it wipes binary logs and resets GTID_EXECUTED.

On production systems, provision a fresh replica or clone of the primary, apply the dump, then start replication. This avoids data loss risk caused by RESET MASTER.

Common Scenarios and Solutions

Importing Logical Dump - Restore the dump on a new server created with --initialize-insecure so GTID_EXECUTED starts empty, then run the included SET GLOBAL GTID_PURGED.

Re-configuring a Replica - Stop replication, run RESET MASTER, restore a fresh dump, set GTID_PURGED, then start replication with CHANGE MASTER TO MASTER_AUTO_POSITION=1.

Best Practices to Avoid This Error

Always restore GTID-based backups on clean instances. Automate environment creation with containers or cloud snapshots so GTID_EXECUTED is guaranteed empty.

Before running SET GLOBAL GTID_PURGED in scripts, check SELECT @@GLOBAL.GTID_EXECUTED. Abort if the result is not empty to prevent runtime failures.

Related Errors and Solutions

Error 1236: MASTER_LOG_POS invalid often follows an incorrect GTID_PURGED operation. Resynchronize by running RESET SLAVE ALL and CHANGE MASTER TO with AUTO_POSITION.

Error 1712: Slave SQL running but encountering GTID gaps can occur when GTID_PURGED was mis-set. Rebuild the replica from a fresh snapshot to eliminate inconsistencies.

Common Causes

Non-empty GTID_EXECUTED set

The server already executed transactions, so GTID_EXECUTED is not empty when SET GLOBAL GTID_PURGED runs.

Restoring dumps on active servers

Dumps made with --set-gtid-purged=ON include SET GLOBAL GTID_PURGED, which fails on servers with existing GTIDs.

Reusing replicas without cleanup

Resetting replication metadata but forgetting RESET MASTER leaves GTID_EXECUTED populated, triggering the error.

Related Errors

MySQL Error 1236: MASTER_LOG_POS invalid

Shows when replication starts from an incorrect binary log position.

MySQL Error 1712: SLAVE_SQL_RUNNING but GTID gap

Indicates missing GTID transactions on a replica, often after wrong GTID_PURGED settings.

MySQL Error 1772: ER_GTID_PURGED_WAS_CHANGED

Fires if GTID_PURGED is altered after transactions exist, similar root cause.

FAQs

Can I bypass Error 1840 without RESET MASTER?

No. GTID_EXECUTED must be empty. Use a fresh instance or RESET MASTER.

Will RESET MASTER delete data?

It removes binary logs and GTID metadata but leaves table data intact. Always back up first.

Is Error 1840 server or client side?

Server side. MySQL rejects the SET GLOBAL GTID_PURGED statement.

How does Galaxy help avoid this error?

Galaxy surfaces GTID variables in the editor sidebar, warns if GTID_EXECUTED is non-empty, and suggests safe execution plans.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo