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.

Since: 1.0

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, Integer batchSize)Deletes records older than the specified number of days with customizable date field, atomicity, and batch size.

Method Details

deleteAllRecords

apex
global static void deleteAllRecords(SObjectType sObjectType)

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

Parameters:

  • sObjectType (SObjectType) - The SObject type containing records to delete.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteAllRecords(Account.SObjectType);

deleteAllRecords

apex
global static void deleteAllRecords(SObjectType sObjectType, Boolean isAtomic)

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

Parameters:

  • sObjectType (SObjectType) - The SObject type containing records to delete.
  • isAtomic (Boolean) - If true, ensures the entire operation is atomic; if false, allows partial deletes.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteAllRecords(Account.SObjectType, true);

deleteAllRecords

apex
global static void deleteAllRecords(String objectApiName)

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

Parameters:

  • objectApiName (String) - The API name of the SObject containing the records to delete.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteAllRecords('Account');

deleteAllRecords

apex
global static void deleteAllRecords(String objectApiName, Boolean isAtomic)

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

Parameters:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • isAtomic (Boolean) - If true, ensures the entire operation is atomic; if false, allows partial deletes.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteAllRecords('Contact', true);
System.debug('All Contact records deleted atomically');

deleteAllRecords

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:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • isAtomic (Boolean) - If true, ensures the entire operation is atomic; if false, allows partial deletes.
  • batchSize (Integer) - The size of each batch for processing records. Defaults to maximum batch size if null.

Since: 1.0

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:

  • sObjectType (SObjectType) - The SObject type containing records to delete.
  • daysThreshold (Integer) - Records older than this number of days will be deleted.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteOlderThanNDays(Task.SObjectType, 30);

deleteOlderThanNDays

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:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • daysThreshold (Integer) - Records older than this number of days will be deleted.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 30);
System.debug('Deleted Task records older than 30 days');

deleteOlderThanNDays

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:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • dateFieldApiName (String) - The API name of the date field used for filtering records.
  • daysThreshold (Integer) - Records with a date field value older than this number of days will be deleted.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 'CreatedDate', 30);

deleteOlderThanNDays

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:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • dateFieldApiName (String) - The API name of the date field used for filtering records.
  • daysThreshold (Integer) - Records with a date field value older than this number of days will be deleted.
  • isAtomic (Boolean) - If true, ensures the entire operation is atomic; if false, allows partial deletes.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Task', 'CreatedDate', 30, true);

deleteOlderThanNDays

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:

  • objectApiName (String) - The API name of the SObject containing the records to delete.
  • dateFieldApiName (String) - The API name of the date field to filter records by. Defaults to CreatedDate if null.
  • daysThreshold (Integer) - Records older than this number of days will be deleted.
  • isAtomic (Boolean) - If true, ensures the entire operation is atomic; if false, allows partial deletes.
  • batchSize (Integer) - The size of each batch for processing records. Defaults to maximum batch size.

Since: 1.0

Example:

apex
UTIL_PurgeRecords.deleteOlderThanNDays('Opportunity', 'CloseDate', 180, false, 100);
System.debug('Deleted Opportunity records older than 180 days in batches of 100');