+ Start a Discussion

Deletion of dulicate records from database

can any body tell me how to delete the duplicate records from the database at one stroke.


I have collected the all duplicate records in a list and when I am trying to delete the list using

"delete list" . Instead of cleaning up it is again n again throwing an exception called "mulitile DML statements with same ids".


Sounds like one or more records you are trying to delete is getting into the list more than once - Lists don't enforce uniqueness.


Sets do enforce uniqueness -  I would try add all the records from the list into a set, and compare the size of the set to the list - if the set is smaller, you'll need to find a way to enforce uniqueness in the list before executing a delete statement


You can add all of the records from the list into a Map. Maps are definitely unique as well. As a bonus, you don't have to work out anything special at all. Actually, you can do it even easier than that:


Set<id> idsToDelete = new set<id>(); for(sobject s:listtodelete) idstodelete.add(s.get('id')); list<id> finalListToDelete = new list<id>(); finalListToDelete.addAll(s); delete finalListToDelete;