Skip to content
Back to skills

145 Cleanup Rollback Guide Bca14308

ASecurity

Strategies for test data isolation and cleanup.

  • 9 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added October 11, 2026
databasesbashdatabase

Works with

  • cli

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 145-cleanup-rollback-guide_bca14308 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 145 Cleanup Rollback Guide Bca14308?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 145 Cleanup Rollback Guide Bca14308
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-145-cleanup-rollback-guide-bca14308/badge)](https://www.skillsdirectory.com/skills/tools-only-145-cleanup-rollback-guide-bca14308)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Cleanup and Rollback Guide

Strategies for test data isolation and cleanup.

## Savepoint/Rollback Pattern

Best for synchronous test isolation.

```apex
// 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

```apex
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

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

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

## Cleanup via sf CLI

```bash
# 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<Id>
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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.