[SalesForce] How do i Find and Delete Duplicates in Custom Object using SOQL or APEX

We are novice to SFDC. We have Custom object for Orders.
that is Order__c.

We tried following SOQL using Workbench

SELECT colname,count(id) num  FROM Order__c group by colname  having count(colname ) > 1 limit 200

Output:

We received 200 records duplicate in our custom object.

Aim is : How do i delete duplicates while keeping one record using workbench using soql or Apex.

Case 2: Our problem becomes shifted into Bulk duplicate which is more than 50,000.

We Tried on Structure of Batch Class and want to write logic for duplication over custom object.

Here is sample code :

Want to put above Logic Apex Code into Batch class. So that it will allow Duplicates more than Governor Limits

global class ExampleBatchClass implements Database.Batchable<sObject>{

        global ExampleBatchClass(){
                   // Batch Constructor
        }

        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
         return Database.getQueryLocator(query);
        }

      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){
              // Logic to be Executed batch wise      

       }

       global void finish(Database.BatchableContext BC){
            // Logic to be Executed at finish
       }
    }

Also,We would like to understand Batch Mechanism because Now,we have to deal with more than 50,000 Duplicated records.

Your valuable time is always appreciated on it ?

Thanks in Advance!

Best Answer

Assuming you don't have too many records, that is, within the SOQL records limit. The limit is 50,000 rows I think. If you have more than that, you will probably need batch to handle this scenario.

Apex code:

List<Order__c> fullList = [Select Id, colname From Order__c ];
Set<String> existingValues = new Set<String>(); //Change this to your colname's type
List<Order__c> dupList = new List<Order__c>();

for(Order__c o : fullList)
{
    if(existingValues.contains(o.colname))
    {
        dupList.add(o);
    }
    else
    {
        existingValues.add(o.colname);
    }
}

delete dupList;