To delete duplicate rows in MySQL safely, first decide which copy to keep, preview the rows that would be removed with a SELECT, back up the table, and then run the delete inside a transaction. The most reliable method on MySQL 8 is to number the copies with ROW_NUMBER() and delete every row numbered above 1.
This guide builds on finding duplicates. It shows three delete techniques, a safety checklist, and how to stop duplicates from coming back. Examples target MySQL 8.4 and later.
A duplicate here means the same email. Before writing any delete, decide the keep rule. In this guide we keep the row with the lowest id, which is the oldest one. Another valid rule is keeping the newest or the most complete record, but the rule must be written down before you delete.
CREATE TABLE users_backup AS SELECT * FROM users; or a mysqldump.SELECT first and read the rows it returns.The backup is cheap compared with restoring from yesterday's dump.
Use the same logic you plan to delete with, but as a SELECT.
These three rows are the extra copies. If this list looks wrong, change the PARTITION BY or ORDER BY before touching any data.
The self join deletes any row that has an older row with the same email.
The multi table DELETE u FROM ... JOIN ... syntax is MySQL specific and works on all supported versions. Join conditions need an index on email to run fast on big tables; without one MySQL compares many rows. If the remaining count looks wrong, run ROLLBACK; instead of COMMIT;.
MySQL does not allow a plain DELETE to select from the same table in a subquery, error 1093. Wrap the numbering in a derived table and join to it.
This method lets you express complex keep rules, such as keeping the newest row by changing the window to ORDER BY created_at DESC, id DESC. It needs MySQL 8.0 or newer.
For very large tables where deleting millions of rows is slow, copy the rows you want to keep into a new table and swap.
RENAME TABLE swaps both names in one atomic step, so readers never see a missing table. Keep users_old until you have verified the result, then drop it. Check that foreign keys, triggers and grants on the old table are recreated on the new one, because LIKE copies columns and indexes but not foreign keys.
Once clean, add a unique index so MySQL rejects repeats.
Inserts that repeat an email now fail with error 1062. To ignore or merge them deliberately, use INSERT ... ON DUPLICATE KEY UPDATE. Avoid INSERT IGNORE as a general habit, since it also hides other errors as warnings.
autocommit is on: without START TRANSACTION, each statement commits at once.DELETE ... LIMIT without an ORDER BY, which removes an arbitrary set of rows.Use ROW_NUMBER() with ORDER BY created_at DESC, id DESC and delete rows numbered above 1. Preview the result with a SELECT first.
MySQL does not let you select from the table you are deleting from inside a subquery. Use a multi table DELETE with a join, or wrap the subquery in a derived table.
Only before you commit, by running ROLLBACK; in the same transaction. After a commit you must restore from a backup or binary log.
For a small share of duplicates, delete in batches. When most rows are removed or the table is huge, copying the wanted rows to a new table and renaming is often faster.
Decide which copy to keep, back up, preview the extra rows with SELECT, then delete inside a transaction using a self join or ROW_NUMBER() in a derived table. For huge tables, rebuild the table and swap names. Finish by adding a unique index so duplicates cannot be inserted again.