Print this page

Permanently delete records from salesforce

Knowledge Article Number 000135460
Description
How to accomplish the same effect of "hard delete" in an apex batch class DML operation? 
Would simply adding "database.emptyRecycleBin([list of items]) after "database.delete([list of items]) 
accomplish the same results as the Data loader "hard delete?"
Resolution
It is possible to hard delete using DataBase.emptyRecycleBin method in the Batch class. Create a sample Batch class as mentioned below and use DataBase.emptyRecycleBin method in the Batch class.

global class BatchDeletion implements Database.Batchable<sObject>, Schedulable 
{   
    
    
    global BatchDeletion()
    {
           //constuctor  
    }
        
        
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        //query to return all expired Case Share records         
        return Database.getQueryLocator([Select id from Account where Name='Test Account12']);
    } 
    
    global void execute(SchedulableContext sc)  
    {   
        //execute the batch
        BatchDeletion deleteCS = new BatchDeletion();
        ID batchprocessid = Database.executeBatch(deleteCS);
    }
    
    global void execute(Database.BatchableContext BC, list<sObject> scope)
    {     
      System.debug('## deleting '+scope.size()+' case share recs');   
 
        //delete list of expired Case Share records
            delete scope;   
            DataBase.emptyRecycleBin(scope); 
    }
    
    
    global void finish(Database.BatchableContext BC) 
    {                 
        //no post processing
       /* System.debug('## Batch Job Finished ##');
        UpdateAccountFields m = new UpdateAccountFields ();
        String sch = '20 30 8 10 2 ?';
        system.schedule('Merge Job', sch, m);*/
       
    }
}

 




promote demote