Am I able to apply Dan Appleman solution to chain my batch job from finish method of another batch job using the code below? Will the Batch job iterate over 50K again as infinity loop or not?

public void finish(Database.BatchableContext BC){

        Database.executeBatch(new UtilizationDetail_DeleteBatch(),100);
        System.debug('Start method signalled that no need to chain another batch another batch job ');


It's fine to chain a batch job from another batch job's finish method. In most cases, this will not result in infinite recursion, although this is dependent on the design of your query and any logic behind performing the chained invocation.

When the second batch job executes, it will execute its own query or query locator in the start() method, and iterate over batches from that query. The results of any database modifications made by the original batch job will be reflected. Since you seem to be working with a deletion job, the records deleted by the first batch job won't present when the second batch job queries against the object.

Note however that a batch job is not subject to the SOQL row limit when using a Query Locator, so it's possible you do not need to chain at all:

If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org

Lastly, if you are performing a one-time deletion as a clean up process, you may wish to simply use the Bulk API via the Data Loader rather than writing a batch class. There are some consequences involved if your architecture involves regularly creating and deleting six figures' worth of objects.


Yes, you can choose to chain jobs ad infinitum as long as you need to. Please note that sandboxes are limited and will eventually terminate the batch after a number of chains, but in production, it can run indefinitely until aborted or it decides not to run again. Note that you should prefer to use a QueryLocator when possible so that you don't need to run more chains than necessary. Also note that the finish method is limited to calling Database.executeBatch only once; any further calls in the same finish method will cause a fatal exception and terminate the batch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.