LoginSignup
2
5

More than 5 years have passed since last update.

【Salesforce】データの一括削除の方法

Last updated at Posted at 2017-03-21

カスタムオブジェクトのtruncate

ビルド > カスタマイズ > ユーザインターフェースより以下をチェック

  • カスタムオブジェクトの切り捨てを有効化

ビルド > 作成 > オブジェクト > オブジェクト名をクリック
「切り捨て」ボタンをクリックすることで削除できます。

標準オブジェクトをtruncateしたい場合

管理 > データの管理 > 一括削除

から可能ですが大量データの削除には向いていません。

Apexバッチで削除用の処理を書くと楽に削除できます。
ただしカスタムオブジェクトの切り捨てに比べると時間はかかります。

DeleteRecord.cls
public with sharing class DeleteRecord implements Database.Batchable<sObject>, Database.Stateful {
    String query;
    public DeleteRecord(String query) {
        this.query = query;
    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(this.query);
    }

    public void execute(Database.BatchableContext BC, List<sObject> scope) {
        delete scope;
        DataBase.emptyRecycleBin(scope);
    }

    public void finish(Database.BatchableContext BC) {

    }
}

実行方法

開発者コンソール > Debug > Open Execute Anonymous Window より

DeleteRecord b = new DeleteRecord('select Id from Account');
Database.executeBatch(b, 2000);
2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5