Batch data cleanup patterns

From PranavNagrecha/AwesomeSalesforceSkills · View interactive version

Batch data cleanup patterns is an AI agent skill from the GitHub repository PranavNagrecha/AwesomeSalesforceSkills. Use when scheduling automated deletion of temporary records, enforcing data retention policies, running nightly cleanup jobs, reclaiming org storage, managing recycle bin, or performing async bulk deletion of aged records. Trigger keywords: batch delete, retention policy, purge records, cleanup job,

How to use this skill

Skill file

Batch Data Cleanup Patterns

This skill activates when records must be deleted on a schedule — retention enforcement, purging temporary or staging records, or permanent removal for compliance — without keeping a copy elsewhere. Use data/data-archival-strategies when records must be preserved in a big object or external store first.


Before Starting

Gather this context before working on anything in this domain:


Questions to Ask Before Configuring

| Ask | Why it matters | What a good answer adds |

|---|---|---|

| "Must deleted records be unrecoverable, or is a 15-day restore window acceptable?" | A plain delete leaves records restorable in the Recycle Bin for 15 days (Gotcha 1) | A per-chunk emptyRecycleBin or a Bulk API 2.0 hardDelete job, only when policy needs it |

| "Is the cutoff exactly N days back, or by calendar year?" | LAST_N_YEARS:n is calendar-year based, so it does not mean "older than n×365 days" (Gotcha 4) | A bound Date cutoff computed in Apex, or a documented calendar rule |

| "Which master-detail children and field history go with these records, and does anyone need them?" | Deleting a parent deletes its detail records and its field history (Gotcha 2) | A child count and an export or archive decision before the first run |

| "How many records will one run delete?" | emptyRecycleBin accepts 10,000 records per call and counts as a DML statement (Gotcha 3) | Per-chunk purging and a batch size that fits |

| "Will anyone deploy changes to the cleanup class while it is scheduled?" | By default you can't deploy an Apex class that has active jobs (Gotcha 5) | A runbook step to pause the job, or the deployment setting turned on deliberately |

| "Could this job be launched from a trigger or by several schedules at once?" | Only five batch jobs run at a time and the flex queue holds 100 (Gotcha 6) | One dispatcher schedule instead of per-record launches |

| "Does the cleanup release include destructive metadata changes?" | A roll-up summary field deleted through Metadata API is purged with no Recycle Bin, and purgeOnDelete doesn't work in production (Gotcha 7) | A one-way-door step in the runbook with exported values |

A proper cleanup design adds a cutoff that means what the policy says, failure logging per record, and a restore decision made up front; a quick delete job leaves recoverable data the policy said was gone, or deletes children nobody counted.


Core Concepts

Batch Apex for deletion

start() returns a Database.QueryLocator (up to 50 million records), execute() receives chunks (200 by default; a QueryLocator scope above 2,000 is chunked to 2,000), and each execute() is its own transaction with its own limits, including 10,000 DML rows. Use Database.delete(scope, false) so one failing record does not roll back the chunk, and record every failed DeleteResult.

Recycle Bin, purge, and hard delete

| Mechanism | Restorable afterwards? | Notes |

|---|---|---|

| delete / Database.delete | Yes, for 15 days | Rows still have to be excluded from queries; they don't count against storage |

| Database.emptyRecycleBin(ids) | No | 10,000 records per call; counts as a DML statement; don't include cascade-deleted child Ids |

| Bulk API 2.0 hardDelete | No | Records skip the Recycle Bin; needs "Bulk API Hard Delete"; recommended by the LDV guide for deletes of a million or more |

Scheduling

A Schedulable class calls Database.executeBatch; System.schedule(name, cron, instance) registers it. Cron syntax is Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year in the scheduling user's time zone, so '0 0 2 * * ?' is 2:00 AM daily. An org can have 100 scheduled Apex jobs at one time.

Deploying the class

Every class ships with a .cls-meta.xml. Because a scheduled cleanup keeps a job active against the class, plan deployments around it.

<?xml version="1.0" encoding="UTF-8"?>
<!-- classes/RetentionPurgeBatch.cls-meta.xml -->
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>67.0</apiVersion>
    <status>Active</status>
</ApexClass>

Common Patterns

Pattern 1: Nightly purge with per-chunk Recycle Bin purge

public with sharing class RetentionPurgeBatch implements Database.Batchable<SObject>, Database.Stateful {
    private final Date cutoff;
    private Integer deleted = 0;
    private Integer failed = 0;

    public RetentionPurgeBatch(Integer retentionDays) {
        this.cutoff = Date.today().addDays(-retentionDays);
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            [SELECT Id FROM Integration_Log__c WHERE CreatedDate < :cutoff]
        );
    }

    public void execute(Database.BatchableContext bc, List<Integration_Log__c> scope) {
        List<Database.DeleteResult> results = Database.delete(scope, false);
        List<Id> purge = new List<Id>();
        for (Integer i = 0; i < results.size(); i++) {
            if (results[i].isSuccess()) {
                purge.add(scope[i].Id);
                deleted++;
            } else {
                failed++;
            }
        }
        if (!purge.isEmpty()) {
            Database.emptyRecycleBin(purge);
        }
    }

    public void finish(Database.BatchableContext bc) {
        System.debug(LoggingLevel.INFO, 'Retention purge: deleted=' + deleted + ' failed=' + failed);
    }
}

The scope stays within the 10,000-record emptyRecycleBin limit because it is purged chunk by chunk; collecting every Id for finish() would not.

Pattern 2: Bulk API 2.0 hard delete for large one-off purges

Create an ingest job with "operation": "hardDelete", upload a CSV with an Id column, close the job (UploadComplete), then read failedResults/. The running user needs "Bulk API Hard Delete". See data/bulk-api-patterns for the job lifecycle.


Decision Guidance

| Situation | Recommended approach | Reason |

|---|---|---|

| Recurring purge, records must be unrecoverable | Batch Apex with per-chunk emptyRecycleBin | Purges within the 10,000-per-call limit, logs each failure |

| One-off purge of a million or more records | Bulk API 2.0 hardDelete | LDV guide recommendation; skips the Recycle Bin |

| Recurring purge, restore window acceptable | Batch Apex with Database.delete(scope, false) only | Simplest; rows are purged automatically after 15 days |

| Parent records with master-detail children | Count children first; lower the batch size or purge children in their own job | Children are deleted with the parent |

| Calendar-year retention policy | LAST_N_YEARS:n with the rule written down | Literal is calendar-aligned |

| Rolling retention policy | Bound Date cutoff computed in Apex | Exact and testable |


Recommended Workflow

  1. Fill templates/batch-data-cleanup-patterns-template.md: object, cutoff rule (rolling or calendar), restore requirement, expected volume, and master-detail children with counts.
  2. Run SELECT COUNT() with the exact cutoff filter and a child-count query in a full-copy sandbox; record both.
  3. Build the batch from Pattern 1 (or a Bulk API 2.0 hardDelete job from Pattern 2; references/worked-example.md walks through both), plus a Schedulable wrapper and .cls-meta.xml files.
  4. Run python3 skills/data/batch-data-cleanup-patterns/scripts/check_batch_data_cleanup_patterns.py --manifest-dir force-app/main/default and fix every ERROR.
  5. Write tests that insert records on both sides of the cutoff, run the batch between Test.startTest() and Test.stopTest() with a scope that covers all test rows, and assert survivors and deletions.
  6. Schedule with System.schedule, confirm the job under Setup > Scheduled Jobs, and add the "pause before deploy" step to the release runbook.

Review Checklist


Salesforce-Specific Gotchas

Full write-ups are in references/gotchas.md:

| # | Gotcha |

|---|---|

| 1 | Deleted is not gone: Recycle Bin rows are restorable for 15 days |

| 2 | Deleting a parent deletes its detail records and its field history |

| 3 | emptyRecycleBin takes 10,000 records per call |

| 4 | LAST_N_YEARS is calendar-aligned |

| 5 | Scheduled jobs block deployments of the class |

| 6 | Batch launches from triggers hit the concurrency limits |

| 7 | Roll-up summary fields deleted through Metadata API are purged at once |

| 8 | Intake purges must hard-delete, and metrics must not live on the purged row |


Output Artifacts

| Artifact | Description |

|---|---|

| Batch Apex class | Retention filter, partial-failure handling, per-chunk purge |

| Schedulable wrapper | Dispatches the batch with an explicit scope |

| .cls-meta.xml files | Deployable class metadata |

| Failure log | Counts or records for failed deletes |

| Filled template | templates/batch-data-cleanup-patterns-template.md |


Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.