function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Parth Patel 184Parth Patel 184 

Delete all records in a custom table

Hi There,
We have a requirement to delete all data in a custom object daily and load it from external data source.

We have a APEX class as per below:

@RestResource(urlMapping='/DeleteRecords/*')
global with sharing class DeleteRecords 
{
@HttpDelete 
    global static void doDelete()
     {
       List<MTDYTD_Trend_Data__c> customObj = [select Id FROM CustomObject LIMIT 10000];
       if(!customObj.isEmpty())
         delete customObj;
     }
}

As we are calling this class from external source and the records are above 10 K it fails. How can we tweak the above to run it in a loop till the entire records in the object is cleared out?

Please advise.

Thank you.

Vishwajeet kumarVishwajeet kumar
Hello,
I think a batch class which deletes records can work and can be executed from this method, give it a try.

Thanks
CharuDuttCharuDutt
Hii Parth Patel
Try The Following Code
public class LeadProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select Id FROM MTDYTD_Trend_Data__c LIMIT 10000');
    }
    public void execute(Database.BatchableContext bc, List<MTDYTD_Trend_Data__c> records){
        
        delete records;
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank you.
Parth Patel 184Parth Patel 184

Thanks Charu.

Can this be exposed as REST?

Rakesh ARakesh A
Hi Parth Patel,

You can call batch apex from rest apex class.