Last active
November 19, 2023 21:18
-
-
Save donners77/ebc0ca013e4f64544631f37b66e5ba74 to your computer and use it in GitHub Desktop.
Bulk delete records in Salesforce using an Apex batch job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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