Loading

Hard Delete Records From Salesforce With Batch Apex

Дата публикации: Jun 18, 2026
Описание
When performing bulk record deletion in Salesforce using an Apex Batch class, the standard database.delete() DML operation moves records to the Recycle Bin rather than permanently removing them from the org. Permanently deleting records without requiring manual emptying of the Recycle Bin is referred to as a "hard delete."
This article explains how to replicate the Data Loader's hard delete behavior using the DataBase.emptyRecycleBin() method in a Batch Apex class.
Решение
To achieve a hard delete using Apex, use the DataBase.emptyRecycleBin(scope) method immediately after delete scope in the execute method of your Batch class. The emptyRecycleBin method permanently removes the deleted records from the Recycle Bin, bypassing the normal 15-day retention period.

The following sample Batch Apex class implements this pattern. It queries Account records with the Name 'Test Account12', deletes them, and then immediately calls Database.emptyRecycleBin(scope) to permanently remove them:
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
       
     }
}
Дополнительные ресурсы

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

Номер статьи базы знаний

000385786

 
Загрузка
Salesforce Help | Article