afv-library/skills/handling-sf-data/references/cleanup-rollback-guide.md
sandipkumar-yadav 37aa84df42
feat: @W-22444026@ Introducing Core Skills, Datacloud Skills, Industries and Utility Skills. (#268)
* Migrating Core Salesforce Skills

* Updating pr comments

* updat reference

* Updating a skill

* Migrating Datacloud skills

* Migrating Industries cloud skills

* Validating - skills fixing

---------

Co-authored-by: Sandip Kumar Yadav <sandipkumar.yadav+sfemu@salesforce.com>
2026-05-14 19:32:15 +05:30

1.7 KiB

Cleanup and Rollback Guide

Strategies for test data isolation and cleanup.

Savepoint/Rollback Pattern

Best for synchronous test isolation.

// Create savepoint BEFORE any DML
Savepoint sp = Database.setSavepoint();

try {
    // Create test data
    List<Account> accounts = TestDataFactory_Account.create(100);

    // Run your tests
    Test.startTest();
    MyClass.processAccounts(accounts);
    Test.stopTest();

    // Assert results
    System.assertEquals(expected, actual);

} finally {
    // Always rollback
    Database.rollback(sp);
}

Limitations:

  • Does not roll back async operations
  • Maximum 5 savepoints per transaction

Cleanup by Name Pattern

String pattern = 'Test%';

DELETE [SELECT Id FROM Opportunity WHERE Name LIKE :pattern];
DELETE [SELECT Id FROM Contact WHERE LastName LIKE :pattern];
DELETE [SELECT Id FROM Account WHERE Name LIKE :pattern];

Order matters: Delete children before parents.

Cleanup by Date

DateTime startTime = DateTime.now().addHours(-1);

DELETE [
    SELECT Id FROM Account
    WHERE CreatedDate >= :startTime
    AND Name LIKE 'Test%'
];

Cleanup via sf CLI

# Export IDs to delete
sf data query \
  --query "SELECT Id FROM Account WHERE Name LIKE 'Test%'" \
  --target-org myorg \
  --result-format csv \
  > delete.csv

# Bulk delete
sf data delete bulk \
  --file delete.csv \
  --sobject Account \
  --target-org myorg \
  --wait 30

Best Practices

  1. Track created IDs - Store in Set
  2. Delete in order - Children first, parents last
  3. Use test prefixes - 'Test', 'BulkTest'
  4. Preview before delete - Verify records first
  5. Use @isTest - Auto-rollback in tests