DELETE FROM $table_name
WHERE row_id NOT IN (
SELECT MAX(row_id) AS max_row_id
FROM $table_name
GROUP BY $column_1, $column_2, … $column_n
); The query will keep only the latest records in the table which represents the unique record based on the columns provided in the GROUP BY clause.