Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Forked from donners77/DeleteRecordsBatchable.cls
Created November 19, 2023 21:18
Show Gist options
  • Save mhamzas/0917193be9fa1d5afb9b7da3ad690b34 to your computer and use it in GitHub Desktop.
Save mhamzas/0917193be9fa1d5afb9b7da3ad690b34 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