Loading

Hard Delete Records From Salesforce With Batch Apex

Publiceringsdatum: Jul 14, 2025
Beskrivning
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?"
Lösning
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 delete account records with name 'Test Account12'        
        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()+' account records');   
 
        //delete list of Account records with name Test Account12
            delete scope;   
            Database.emptyRecycleBin(scope);  
    }
        
    global void finish(Database.BatchableContext BC) 
    {                 
        //no post processing
       
     }
}
 
Ytterligare resurser

For more information on alternate hard delete options, see Delete Unwanted Data in a Salesforce Org.

Batch Apex

Knowledge-artikelnummer

000385786

 
Laddar
Salesforce Help | Article