Skip to content

UTIL_PurgeRecords ​

Class · Group: Bulk DML

Learn it: Utilities guide

apex
global inherited sharing class UTIL_PurgeRecords

Provides utility methods for purging records from Salesforce objects using adaptive async processing. Automatically selects between Queueable and Batch execution based on data volume and governor limits. Supports deleting all records or records older than a specified number of days. All entry points inherit the flag-driven default AccessLevel resolved by DML_SharingProxy.defaultAccessLevel() — under the secure-by-default posture this is USER_MODE, so the invoking user must have delete CRUD on the target object for the purge to succeed. This is deliberate: UTIL_PurgeRecords is global, so any Apex caller in your org can reach it, and silently elevating to SYSTEM_MODE would let a low-privilege user destructively delete records they have no native right to delete. Integrations that genuinely need admin-mandate semantics (for example, cleanup utilities behind a custom permission gate) should construct a PROC_ExecuteDML(operation, allOrNothing, AccessLevel.SYSTEM_MODE) explicitly and pair it with a caller-side permission check; the framework declines to make that elevation implicit. API calls and API issues can be deleted only by a user the BypassApiRecordDeleteGuard feature flag is on for. A built-in rule turns the flag on for every holder of the Delete API Records permission, which the Kern Administrator permission set grants, and your org can widen it to more users. A purge of either, run by anyone else, deletes nothing, starts no job and logs one warning that says so. Schedule such a purge as a user the flag is on for. A purge of API calls leaves out every call that still has an open API issue, because such a call is never deleted, not even by an administrator. The purge reads as the user who runs it, so it leaves out such a call only when that user can read every open issue. The Kern Administrator permission set gives this access. When the user cannot see the open issue of a call, the delete guard refuses that call, one call at a time, and logs each refusal. A user who cannot read API issues is told so in one warning, and the purge of API calls does not start. A call that is left out is purged by a later run, once its issue is resolved or dismissed. A purge of chain runs (AsyncChainExecution__c) by their end time (CompletedAt__c) also deletes a run that stays Stalled, when the time it stalled (StalledAt__c) is older than the same number of days. A Stalled run never gets an end time, so without this rule it would be kept forever. The user who runs the purge must be able to read both the Status and the Stalled At fields of a chain run; the Kern Administrator permission set gives this access. An upgrade does not give a new field to profiles or to permission sets your org made itself. Without that access, the purge deletes by end time only, so every stalled run is kept, and it logs one warning that says so.

Example

apex
UTIL_PurgeRecords.deleteAllRecords(LogEntry__c.SObjectType);
UTIL_PurgeRecords.deleteOlderThanNDays('LogEntry__c', 90);
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 'CreatedDate', 30, false, 200);

See Also: PROC_ExecuteDML, UTIL_FeatureFlag


Methods ​

MethodDescription
global static void deleteAllRecords(SObjectType sObjectType)Deletes all records of the specified SObject type with default non-atomic behavior.
global static void deleteAllRecords(SObjectType sObjectType, Boolean isAtomic)Deletes all records of the specified SObject type with configurable atomicity.
global static void deleteAllRecords(String objectApiName)Deletes all records of the specified SObject type with default non-atomic behavior.
global static void deleteAllRecords(String objectApiName, Boolean isAtomic)Deletes all records of the specified SObject type with specified atomicity.
global static void deleteAllRecords(String objectApiName, Boolean isAtomic, Integer batchSize)Deletes all records of the specified SObject type with customizable atomicity and batch size.
global static void deleteOlderThanNDays(SObjectType sObjectType, Integer daysThreshold)Deletes records older than the specified number of days from the given SObject type, using CreatedDate as the default date field.
global static void deleteOlderThanNDays(String objectApiName, Integer daysThreshold)Deletes records older than the specified number of days from the given SObject type, using CreatedDate as the default date field.
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold)Deletes records older than the specified number of days based on the specified date field.
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold, Boolean isAtomic)Deletes records older than the specified number of days with configurable atomicity.
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold, Boolean isAtomic, Integer batchSize)Deletes records older than the specified number of days with customizable date field, atomicity, and batch size.

deleteAllRecords ​

apex
global static void deleteAllRecords(SObjectType sObjectType)

Deletes all records of the specified SObject type with default non-atomic behavior.

Parameters

ParameterTypeDescription
sObjectTypeSObjectTypeThe SObject type containing records to delete.

Example

apex
UTIL_PurgeRecords.deleteAllRecords(Account.SObjectType);
apex
global static void deleteAllRecords(SObjectType sObjectType, Boolean isAtomic)

Deletes all records of the specified SObject type with configurable atomicity.

Parameters

ParameterTypeDescription
sObjectTypeSObjectTypeThe SObject type containing records to delete.
isAtomicBooleanIf true, ensures the entire operation is atomic; if false, allows partial deletes.

Example

apex
UTIL_PurgeRecords.deleteAllRecords(Account.SObjectType, true);
apex
global static void deleteAllRecords(String objectApiName)

Deletes all records of the specified SObject type with default non-atomic behavior.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.

Example

apex
UTIL_PurgeRecords.deleteAllRecords('Account');
apex
global static void deleteAllRecords(String objectApiName, Boolean isAtomic)

Deletes all records of the specified SObject type with specified atomicity.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
isAtomicBooleanIf true, ensures the entire operation is atomic; if false, allows partial deletes.

Example

apex
UTIL_PurgeRecords.deleteAllRecords('Contact', true);
// An asynchronous job now deletes the Contact records, so they are not gone yet when this line runs.
apex
global static void deleteAllRecords(String objectApiName, Boolean isAtomic, Integer batchSize)

Deletes all records of the specified SObject type with customizable atomicity and batch size.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
isAtomicBooleanIf true, ensures the entire operation is atomic; if false, allows partial deletes.
batchSizeIntegerThe size of each batch for processing records. Defaults to maximum batch size if null.

Example

apex
UTIL_PurgeRecords.deleteAllRecords('Lead', false, 200);
// An asynchronous job now deletes the Lead records, 200 records in each batch.

deleteOlderThanNDays ​

apex
global static void deleteOlderThanNDays(SObjectType sObjectType, Integer daysThreshold)

Deletes records older than the specified number of days from the given SObject type, using CreatedDate as the default date field.

Parameters

ParameterTypeDescription
sObjectTypeSObjectTypeThe SObject type containing records to delete.
daysThresholdIntegerRecords older than this number of days will be deleted.

Example

apex
UTIL_PurgeRecords.deleteOlderThanNDays(Task.SObjectType, 30);
apex
global static void deleteOlderThanNDays(String objectApiName, Integer daysThreshold)

Deletes records older than the specified number of days from the given SObject type, using CreatedDate as the default date field.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
daysThresholdIntegerRecords older than this number of days will be deleted.

Example

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 30);
// An asynchronous job now deletes the Task records created more than 30 days ago.
apex
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold)

Deletes records older than the specified number of days based on the specified date field.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
dateFieldApiNameStringThe API name of the date field used for filtering records.
daysThresholdIntegerRecords with a date field value older than this number of days will be deleted.

Example

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 'CreatedDate', 30);
apex
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold, Boolean isAtomic)

Deletes records older than the specified number of days with configurable atomicity.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
dateFieldApiNameStringThe API name of the date field used for filtering records.
daysThresholdIntegerRecords with a date field value older than this number of days will be deleted.
isAtomicBooleanIf true, ensures the entire operation is atomic; if false, allows partial deletes.

Example

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 'CreatedDate', 30, true);
apex
global static void deleteOlderThanNDays(String objectApiName, String dateFieldApiName, Integer daysThreshold, Boolean isAtomic, Integer batchSize)

Deletes records older than the specified number of days with customizable date field, atomicity, and batch size.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject containing the records to delete.
dateFieldApiNameStringThe API name of the date field to filter records by. Defaults to CreatedDate if null.
daysThresholdIntegerRecords older than this number of days will be deleted.
isAtomicBooleanIf true, ensures the entire operation is atomic; if false, allows partial deletes.
batchSizeIntegerThe size of each batch for processing records. Defaults to maximum batch size.

Example

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Opportunity', 'CloseDate', 180, false, 100);
// An asynchronous job now deletes the Opportunity records whose CloseDate is more than 180 days ago, 100 records in each batch.