In my salesforce org, I have a list of Accounts which was loaded externally using CSV file using Data loader tool. The problem is that 3 team member of my team uploaded same data thrice and thats why duplicates Account data coming. I need to remove all those multi-duplicate account.

How can I remove that? What is the correct way of doing this rectification? I assume my org might have 1000 unique Accounts, so due to developers error, count has become 3000. This is terrible.

enter image description here


4 Answers 4


If you're only talking about 3000 records and you're only worried about exact name dups (ignoring case), a primitive script would also work:

List<Account> allAccounts = [SELECT Name FROM Account];
Set<String> accountNames = new Set<String>();
List<Account> accountsToDelete = new List<Account>();
for (Account a : allAccounts) {
    if (accountNames.contains(a.Name)) {
    else {
delete accountsToDelete;
  • Thanks. How we can considering using ignore case ? May 23, 2017 at 20:30
  • From your screenshot, it looks like all the names are identical matches, so the answer I provided would be appropriate. However - to answer your follow up question, you could use containsIgnoreCase() instead of contains() May 23, 2017 at 20:42

If you haven't done anything with them, it might just be easier to delete all 3,000 records and re-import. As an alternative, you can use Duplicate Rules to automatically identify all the duplicates, now and in the future, and even write some Execute Anonymous code to remove all the duplicates:

DuplicateRecordSet[] drs = [SELECT (SELECT RecordId FROM DuplicateRecordItems) FROM DuplicateRecordSet WHERE SObjectType='Account'];
Account[] accounts = new Account[0];
for(DuplicateRecordSet dupe: drs) {
  // We use 1 here to ignore the first entry
  for(Integer i = 1; i < dupe.DuplicateRecordItems.size(); i++) {
    accounts.add(new Account(Id=dupe.DuplicateRecordItems[i].RecordId));
delete accounts;

For what its worth, Duplicate Rules can also be configured to catch fuzzy matches (e.g. "The Gap" versus "Gap, The", as well as case variations).

  • SELECT (SELECT RecordId FROM DuplicateRecordItems) FROM DuplicateRecordSet WHERE SObjectType='Account' - This query does not pull any records. May 26, 2017 at 11:05
  • @user4567570 You won't get any results until you configure duplicate rules in the setup screen and allow initial processing to finish. The code I demonstrated here works in my developer org.
    – sfdcfox
    May 26, 2017 at 12:35

If you don't have the success file install Dupcatcher and merge the accounts


You can simply pull out the success log file which is getting stored into this system and delete those ids from data loader delete option.

Not the answer you're looking for? Browse other questions tagged .