There is a object(assume A) which will store event logs for a custom object(assume B). A batch job, runs weekly on object A to delete the records based on some conditions. It throws an exception like below:

System.LimitException: Too many query locator rows: 50000001

What is the recommended solution for this exception. There is already some WHERE clause in the SOQL query, but the matching record count is high.

Can we control this by keeping LIMIT 50000000 in dynamic query? But how to process all records in batch apex without skipping the records?

1 Answer 1


I suggest that the "quick fix" is running the batch job more frequently e.g. every hour, assuming that the records build up over the day.

But inserting and deleting such large number of objects a day begs the question of what you are trying to achieve here? Other approaches may be much, much more efficient. Suggest you add an explanation to your question of what the purpose of this logic is.

Also, be careful about the zeroes; the exception is 50 million rows but your LIMIT 5000000 is only 5 million rows.

  • will bulk api delete through workbench or something help here?
    – RedDevil
    Commented May 13, 2019 at 8:36
  • Thanks, I think this will work as you suggested running the job frequently. Currently it's running weekly, will make it to run daily. (Sorry I've written daily in the question, updated now)
    – Lokesh
    Commented May 13, 2019 at 8:38
  • Hi @RedDevil I don't know - worth posting as an answer though.
    – Keith C
    Commented May 13, 2019 at 8:43
  • @KeithC, was just curious and have never done it before. I will try a small POC
    – RedDevil
    Commented May 13, 2019 at 8:44
  • 1
    @RedDevil Will avoid that governor limit but does presumably require something external to be scheduled. Think the key question here is whether all the inserts and deletes make sense in the first place...
    – Keith C
    Commented May 13, 2019 at 8:47

You must log in to answer this question.

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