0

We have to delete nearly a million record on a daily basis. We currently have a batch which is scheduled and it deletes all the records. The batch is taking too much of time to delete these records. Is there any other way other than batch that deletes all these records with lesser time compared to the batch.

Thanks in advance

4
  • 2
    What batch chunk size are you using, how many relationships are there to these records (master detail or set null relationships) and how much automation do you have hung off the deletion of these records?
    – Phil W
    Commented Sep 22, 2022 at 16:22
  • 3
    Are you sure there is no alternative to create this million records on a daily basis in the first place? There might be some edge cases, but it might well be an example of XY problem. Maybe you could describe a little bit the context, in which this problem appears? Commented Sep 22, 2022 at 17:41
  • 3
    What is your use case of having to create such a huge volume of records and then to delete them on a daily basis? Commented Sep 22, 2022 at 18:01
  • 2
    see stackoverflow.com/questions/56969396/…
    – cropredy
    Commented Sep 23, 2022 at 4:23

2 Answers 2

2

The fastest way would be to use an external API, which you could parallelize quite easily (but be aware of concurrent limits). As far as speeding up the batch, it won't go any faster than asynchronous code allows. Instead, you need to optimize any trigger logic, etc, so that you can delete records as fast as possible from the batch context, and set the batch's scope size to 2,000 to minimize the number of executions it needs.

0

The use case is still not clear on what exactly is creating 1M records per day that you want to delete later. But assuming you have a scenario (really large enterprise) to store millions of records on Salesforce and then to delete a subset of it on a specific date, you need to check few things:

  • For the job taking long time - what is it that is causing the issue? Is it locking records, delaying something else?
  • Do the 1M records have child records/ When you issue a delete, do the deleted records have a parent record?
  • Are you grouping all the deletes by the parent record so the locking is more efficient? -Also, if you are deleting 1M records, is the slowness in the delete? Or in the query? Is your query using an efficient index?

Deletes are generally very fast. Things that cause slowdowns generally revolve around relationships to other objects.

  • If Apex is indeed performing poorly, then another option is to schedule Salesforce Data Loader or an ETL tool to identify the records, and hard delete them using the Bulk API or Bulk API 2.0. See Deleting Data from LDV guide

  • It can be slower to let a master-detail relationship delete child records. You may be better off deleting the children first, and then deleting the parents.

  • If you have parent relationships (especially to Account or an object that is then related to Account), make sure to sort by that relationship field and delete in order.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .