Skip to content

Instantly share code, notes, and snippets.

@donners77
Last active November 19, 2023 21:18
Show Gist options
  • Save donners77/ebc0ca013e4f64544631f37b66e5ba74 to your computer and use it in GitHub Desktop.
Save donners77/ebc0ca013e4f64544631f37b66e5ba74 to your computer and use it in GitHub Desktop.
Bulk delete records in Salesforce using an Apex batch job
// Example use:
// First edit code below and replace ###ObjectName### with Contact
// Then execute in the Developer Console | Debug | Open Execute Anonymous Window
// DeleteRecordsBatchable batchable = new DeleteRecordsBatchable('SELECT Id FROM Contact WHERE Name = \'DeleteMe\'');
// Id batchId = Database.executeBatch(batchable, 10);
global class DeleteRecordsBatchable implements Database.Batchable<sObject> { // created to bulk delete records - use with care!
// Fields
global final String Query;
// Constructor
global DeleteRecordsBatchable(String q) {
Query = q;
}
// Start
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
// Execute
global void execute
( Database.BatchableContext bc
, List<###ObjectName###> scope
) {
delete scope;
}
// Finish
global void finish(Database.BatchableContext bc) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment