How to Fix MySQL Error 1840 (ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY)
You see mysql error 1840 when you try to set the purged transaction list while the executed transaction list already has data. To fix this failure, you must clear the master binary log state before you load your backup file, or remove the transaction state change from your import script. This failure blocks database restores and replica setup workflows. It protects your database from mixing two separate transaction histories. When you know why the engine blocks this action, you can restore your
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You see mysql error 1840 when you try to set the purged transaction list while the executed transaction list already has data. To fix this failure, you must clear the master binary log state before you load your backup file, or remove the transaction state change from your import script.
This failure blocks database restores and replica setup workflows. It protects your database from mixing two separate transaction histories. When you know why the engine blocks this action, you can restore your data without breaking replication.
What causes mysql error 1840 in your database
MySQL uses global transaction identifiers to track every change across a cluster. The database tracks two main lists in global state. One list holds all transactions that ran on the server. The other list holds transactions that ran in the past but were deleted from the binary logs.
The official error symbol is ER_CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY, which maps to SQLSTATE HY000. The engine states that the purged global variable can only be set when the executed global variable is empty. If the target server has run even one transaction, the executed list is not empty.
This problem happens most often during a logical backup restore. Backup tools write a command at the top of the dump file. This command tries to update the purged transaction list on the target server. If the target database is brand new, the restore works fine. If the target database already has tables, users, or system changes, the engine stops the import and throws the error.
How to diagnose and fix the failure in production
First, check the current transaction state on your target database server. Ask an admin with read permissions to inspect the global executed transaction list. If the list shows any identifiers, your server has already recorded local changes.
Next, decide if the target server holds any data you need to keep. Choose your fix based on the role of the target server.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If you are building a fresh replica from a backup, you want the replica to match the source server. In this case, you can reset the master binary log state on the new replica. This action wipes the executed transaction list clean. Still, you must never run a master reset on an active production primary database. Wiping binary logs on a live primary breaks connected replicas and destroys point-in-time recovery logs.
If you are restoring a single table or merging data into a live database, do not reset the binary logs. Instead, change how you create or run the backup file. You can tell your backup tool to skip global transaction statements when it creates the dump file. You can also edit an existing dump file to remove the line that sets the purged list before you run the import.
How to prevent restore errors across your team
Database restore failures often happen when engineers run ad-hoc imports with full admin privileges. A single mistake during a restore can overwrite transaction tracking or corrupt a replica cluster. Teams should standardize how they run backups and imports in production.
Start by separating daily read roles from backup and restore roles. Most engineers only need to read data, where data masking rewrites matched fields like emails, phone numbers, card numbers, or national IDs in results. When an engineer must run a data import, route the request through an approval workflow.
Peer review lets a second engineer check the import file before it runs. The reviewer can spot transaction state changes that would trigger error 1840 or break replication. Logging every command also gives your team a clear audit trail if a restore fails.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What does SQLSTATE HY000 mean for error 1840?
SQLSTATE HY000 is a general error code in MySQL. You must read the specific error number 1840 to know that the failure comes from a non-empty executed transaction list.
Is it safe to reset the master state on a production database?
No, resetting the master state deletes all binary logs and clears the transaction history. Only run a reset on a brand new server or an isolated replica you are rebuilding from scratch.
Why did my backup file include a purged transaction command?
Backup tools add this command by default when global transaction tracking is active on the source server. It helps new replicas know which historical transactions they do not need to fetch.
Related guides
In this post