Salesforce で Apex 一括処理クラスを使用してレコードを一括削除する場合、標準の database.delete() DML操作ではレコードを組織から完全に削除するのではなくごみ箱に移動します。手動でごみ箱を空にする必要なく、レコードを完全に削除することを「物理削除 (Hard Delete)」と呼びます。
この記事では Apex 一括処理クラスで DataBase.emptyRecycleBin() を使用して、データローダーによる Hard Delete を再現する方法を説明します。
Apex 一括処理クラスを使用してレコードを完全に削除するには、Apex 一括処理クラスの execute メソッド内の delete scope の直後に DataBase.emptyRecycleBin(scope) メソッドを使用します。emptyRecycleBin メソッドは、通常の 15 日間の保持期間をスキップして、削除されたレコードをごみ箱から完全に削除します。
次のサンプル Apex 一括処理クラスはこのパターンを実装しています。名前が「Test Account12」の取引先レコードをクエリし、それらを削除してから、すぐに Database.emptyRecycleBin(scope) を呼び出して完全に削除します。
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
}
}
その他の物理削除の選択肢の詳細は以下を参照してください。
000385786

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.