

-- 2) Pick the row to keep per duplicate group (newest/highest id)
CREATE TEMPORARY TABLE keep_ids AS
SELECT MAX(id) AS keep_id
FROM tbl_colorcloud_saves
GROUP BY original_save_id, user_id;

-- 3) Delete the others (after verifying backups)
DELETE s
FROM tbl_colorcloud_saves s
LEFT JOIN keep_ids k ON s.id = k.keep_id
WHERE k.keep_id IS NULL;

-- 4) Add the unique index to prevent future duplicates
ALTER TABLE tbl_colorcloud_saves
  ADD UNIQUE KEY ux_original_user (original_save_id, user_id);
