forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-data-manage
platform-data-manage
VerifiedSalesforce data operations with 130-point scoring. Use to create, update, delete, bulk import/export, generate test data, and clean up org records via sf CLI and anonymous Apex. TRIGGER on creating test data, bulk import/export, sf data CLI commands, data-factory patterns for Apex tests, or seeding/cleaning org records. DO NOT TRIGGER for SOQL query writing only (use platform-soql-query), Apex test execution (use platform-apex-test-run), or metadata deployment (use platform-metadata-deploy).
references/cleanup-rollback-guide.md
View on GitHub<!-- Parent: platform-data-manage/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
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |