Skip to content

UTIL_PurgeRecords

Class · Group: Bulk DML

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 subscriber Apex caller 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.

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


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);
System.debug('All Contact records deleted atomically');
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);
System.debug('All Lead records deleted in batches of 200');

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);
System.debug('Deleted Task records older than 30 days');
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);
System.debug('Deleted Opportunity records older than 180 days in batches of 100');