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
ADC Test UserADC Test User 

Batch Apex - Performance problem deleting large sets of data

Hi -


I am looking for the fastest way possible to mass delete all records for a given custom object. I developed a very simple Batch Apex class which is described below.  The code works but it performs very slowly. Yesterday it took 40 minutes to delete 34,000 records, although I did not run into any governor limits.  It seems that the execute() call is only receiving a few records at a time.  Is there any way to coax it to process large groups of records?


Any help would be greatly appreciated.


global class MassDeleteCustomObject implements Database.Batchable<Sobject>


  global final String query = 'select Id from <my custom object>';


  global Database.QueryLocator start(Database.BatchableContext BC)



       return Database.getQueryLocator(query);



    global void execute(Database.BatchableContext BC, List<Sobject> records)


        delete records;



  global void finish(Database.BatchableContext BC)





The default batch size should be 200 and I believe you can set it lower but not higher. You can add some logs to see exactly how many were processed each time.
ADC Test UserADC Test User

Thanks for the reply.


Even at 200 that's still 170 specific delete calls. Sometimes the number of records to be deleted is around 200,000 so that would be 1,000 individual calls.  Not very efficient, and even still the SOAP API calls at 200 per trip seem to run faster despite the expense of all the round trips!



Not very efficient?  You just deleted tens of thousands of records with fewer than 20 lines of code!  That's pretty darn efficient!

Try using dataloader, exporting all the IDs, and then using that file to delete all the records.  If it's faster, then go for it, and don't worry about this being "slow."


Really, 40 minutes is fairly good; remember that Batch Apex is not a high-priority process on the platform.  It was never meant to be time-critical.  You're waiting for higher-priority things to happen.

If you want to be at the front of the line for CPU cycles, use dataloader or the SOAP API (as you have done).  


I had a similar problem. I needed to delete 2 .5 million records. I identified some partioning criteria and then generated several different CSV files and ran the data loader on multiple machines. 


Whether you use dataloader or batch, delete performance is almost same. I am trying to delete 6m records and can't delete more than 1m in an hour.
Colin KenworthyColin Kenworthy
Also check if triggers are doing anything (like deleting child records) when you delete the records and/or roll up fields.  All are things that might slow up the delete process.

Hi All,
here is how you can do faster deletion :

1. Once you get all the Records in CSV, remove all other details except the IDs.
2. Use dataloader with bulk API Option.

You can delete a million records in less than 4mins !