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