Print this page

Purging AsyncApexJob

Knowledge Article Number 000193840
Description Queries against AsyncApexJob may receive this message:

System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce about custom indexing. 
Completed AsyncApexJob records are automatically purge by salesforce after 7 days. If the number of records grows above 100,000 customers should consider implementing this (from apex docs):


Deletes asynchronous Apex job records for jobs that have finished execution before the specified date with a Completed, Aborted, or Failed status, and returns the number of records deleted.


public static Integer purgeOldAsyncJobs(Date dt)


Type: Date
Specifies the date up to which old records are deleted. The date comparison is based on the CompletedDate field of AsyncApexJob, which is in the GMT time zone.

Return Value

Type: Integer


Asynchronous Apex job records are records in AsyncApexJob.

The system cleans up asynchronous job records for jobs that have finished execution and are older than seven days. You can use this method to further reduce the size of AsyncApexJob by cleaning up more records.

Each execution of this method counts as a single row against the governor limit for DML statements.


This example shows how to delete all job records for jobs that have finished before today’s date.
Integer count = System.purgeOldAsyncJobs
System.debug('Deleted ' + 
   count + ' old jobs.');

promote demote