You need to sign in to do that
Don't have an account?
Giancarlo Amati
scheduled job to delete records
Dear All,
I need to delete like 2M records from our environment and I wonder if there's any way I could do by scheduling a job so not to impact on the performace of our instance.
How can I do it?
Thank you.
GC
I need to delete like 2M records from our environment and I wonder if there's any way I could do by scheduling a job so not to impact on the performace of our instance.
How can I do it?
Thank you.
GC
You can schedule job to delete records using Batch apex.
Below reference links will give you more information.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_iterable.htm
https://salesforce.stackexchange.com/questions/156217/how-to-write-a-batch-class-for-scheduled-deletion
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch.htm
Sample:
======
global class DeleteRecords implements Database.Batchable<string>, Database.Stateful {
global boolean bReRun = false; //will be used to determine if batch has to re-run in case there are more that 10K of records
global Iterable<string> start(Database.BatchableContext ctx) {
return new list<String> { 'object1', 'object2', 'object3','object4','object5'}; //list of strings with my object names
}
global void execute(Database.BatchableContext ctx, list<string> lstsObjectName) {
list<sObject> lstDeleteRecords = new list<sObject>();
for(string strObjectName : lstsObjectName) {
for(sObject objsObject : database.query('Select Id from ' + strObjectName + ' where for_delete__c = TRUE')) {
if(lstDeleteRecords.size() < 9998)
lstDeleteRecords.add(objsObject);
else {
bReRun = true;
break;
}
}
}
lstDeleteRecords.sort();
delete lstDeleteRecords;
}
global void finish(Database.BatchableContext ctx) {
if(bReRun) {
Database.executebatch(new DeleteRecords());
}
}
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks,
Vinay Kumar