Skip to content

forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-data-manage

platform-data-manage

Verified

Salesforce 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).

FileKindSize
README.mdmarkdown4.5 KB
SKILL.mdskill md10.5 KB
assets/bulk/bulk-insert-10000.apexother12.3 KB
assets/bulk/bulk-insert-200.apexother12.4 KB
assets/bulk/bulk-insert-500.apexother13.1 KB
assets/bulk/bulk-upsert-external-id.apexother13.4 KB
assets/cleanup/delete-by-created-date.apexother15.9 KB
assets/cleanup/delete-by-name.apexother12.9 KB
assets/cleanup/delete-test-data.apexother16.6 KB
assets/cleanup/rollback-transaction.apexother12.4 KB
assets/csv/account-import.csvother1.8 KB
assets/csv/contact-import.csvother1.9 KB
assets/csv/custom-object-import.csvother1.1 KB
assets/csv/opportunity-import.csvother1.6 KB
assets/factories/account-factory.apexother5.6 KB
assets/factories/case-factory.apexother8.1 KB
assets/factories/contact-factory.apexother5.9 KB
assets/factories/custom-object-factory.apexother10.8 KB
assets/factories/event-factory.apexother9.6 KB
assets/factories/hierarchy-factory.apexother16.0 KB
assets/factories/lead-factory.apexother6.4 KB
assets/factories/opportunity-factory.apexother7.3 KB
assets/factories/task-factory.apexother8.1 KB
assets/factories/user-factory.apexother8.8 KB
assets/json/account-contact-tree.jsonconfig3.6 KB
assets/json/account-opportunity-tree.jsonconfig3.3 KB
assets/json/full-hierarchy-tree.jsonconfig5.8 KB
assets/soql/aggregate.soqlother12.3 KB
assets/soql/child-to-parent.soqlother9.8 KB
assets/soql/parent-to-child.soqlother8.6 KB
assets/soql/polymorphic.soqlother11.1 KB
assets/soql/subquery.soqlother16.2 KB
references/anonymous-apex-guide.mdmarkdown2.0 KB
references/bulk-operations-guide.mdmarkdown1.8 KB
references/bulk-testing-example.mdmarkdown4.9 KB
references/cleanup-rollback-example.mdmarkdown8.2 KB
references/cleanup-rollback-guide.mdmarkdown1.7 KB
references/crud-workflow-example.mdmarkdown4.0 KB
references/governor-limits-reference.mdmarkdown1.8 KB
references/orchestration.mdmarkdown7.0 KB
references/relationship-query-examples.mdmarkdown5.7 KB
references/sf-cli-data-commands.mdmarkdown2.8 KB
references/soql-relationship-guide.mdmarkdown2.0 KB
references/test-data-best-practices.mdmarkdown3.2 KB
references/test-data-factory-usage.mdmarkdown7.6 KB
references/test-data-patterns.mdmarkdown2.4 KB
scripts/soql_validator.pyscript10.9 KB
scripts/validate_data_operation.pyscript13.5 KB

assets/cleanup/delete-by-name.apex

View on GitHub
/**
 * ═══════════════════════════════════════════════════════════════════════════════
 * DELETE RECORDS BY NAME PATTERN
 * Clean up test records matching a specific naming pattern
 * ═══════════════════════════════════════════════════════════════════════════════
 *
 * PURPOSE:
 * Delete test records that follow a naming convention.
 * Common patterns: 'Test%', 'BulkTest%', 'Demo%', etc.
 *
 * ⚠️  WARNING:
 * Always PREVIEW records before deleting!
 * Records go to Recycle Bin by default (soft delete)
 *
 * ═══════════════════════════════════════════════════════════════════════════════
 */

// ═══════════════════════════════════════════════════════════════════════════════
// CONFIGURATION
// ═══════════════════════════════════════════════════════════════════════════════

String namePattern = 'Test%';  // LIKE pattern for Name field
Boolean previewOnly = true;    // Set to false to actually delete
Integer maxRecords = 10000;    // Safety limit

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 1: PREVIEW ACCOUNTS
// ═══════════════════════════════════════════════════════════════════════════════

System.debug('═══════════════════════════════════════════════════════════════');
System.debug('DELETE BY NAME PATTERN: ' + namePattern);
System.debug('Mode: ' + (previewOnly ? 'PREVIEW ONLY' : '⚠️  DELETE MODE'));
System.debug('═══════════════════════════════════════════════════════════════');
System.debug('');

// Query Accounts
List<Account> accountsToDelete = [
    SELECT Id, Name, CreatedDate, CreatedBy.Name
    FROM Account
    WHERE Name LIKE :namePattern
    ORDER BY CreatedDate DESC
    LIMIT :maxRecords
];

System.debug('📋 ACCOUNTS matching "' + namePattern + '": ' + accountsToDelete.size());
if (!accountsToDelete.isEmpty()) {
    System.debug('   Sample records:');
    for (Integer i = 0; i < Math.min(10, accountsToDelete.size()); i++) {
        Account acc = accountsToDelete[i];
        System.debug('   - ' + acc.Name + ' (Created: ' + acc.CreatedDate.format() + ' by ' + acc.CreatedBy.Name + ')');
    }
    if (accountsToDelete.size() > 10) {
        System.debug('   ... and ' + (accountsToDelete.size() - 10) + ' more');
    }
}
System.debug('');

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 2: PREVIEW CONTACTS
// ═══════════════════════════════════════════════════════════════════════════════

List<Contact> contactsToDelete = [
    SELECT Id, FirstName, LastName, Account.Name, CreatedDate
    FROM Contact
    WHERE LastName LIKE :namePattern
    OR Account.Name LIKE :namePattern
    ORDER BY CreatedDate DESC
    LIMIT :maxRecords
];

System.debug('📋 CONTACTS matching "' + namePattern + '": ' + contactsToDelete.size());
if (!contactsToDelete.isEmpty()) {
    System.debug('   Sample records:');
    for (Integer i = 0; i < Math.min(10, contactsToDelete.size()); i++) {
        Contact con = contactsToDelete[i];
        System.debug('   - ' + con.FirstName + ' ' + con.LastName + ' (' + con.Account?.Name + ')');
    }
}
System.debug('');

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 3: PREVIEW OPPORTUNITIES
// ═══════════════════════════════════════════════════════════════════════════════

List<Opportunity> opportunitiesToDelete = [
    SELECT Id, Name, Account.Name, StageName, Amount, CreatedDate
    FROM Opportunity
    WHERE Name LIKE :namePattern
    OR Account.Name LIKE :namePattern
    ORDER BY CreatedDate DESC
    LIMIT :maxRecords
];

System.debug('📋 OPPORTUNITIES matching "' + namePattern + '": ' + opportunitiesToDelete.size());
if (!opportunitiesToDelete.isEmpty()) {
    System.debug('   Sample records:');
    for (Integer i = 0; i < Math.min(10, opportunitiesToDelete.size()); i++) {
        Opportunity opp = opportunitiesToDelete[i];
        System.debug('   - ' + opp.Name + ' (' + opp.StageName + ', $' + opp.Amount + ')');
    }
}
System.debug('');

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 4: PREVIEW CASES
// ═══════════════════════════════════════════════════════════════════════════════

List<Case> casesToDelete = [
    SELECT Id, CaseNumber, Subject, Account.Name, Status, CreatedDate
    FROM Case
    WHERE Subject LIKE :namePattern
    OR Account.Name LIKE :namePattern
    ORDER BY CreatedDate DESC
    LIMIT :maxRecords
];

System.debug('📋 CASES matching "' + namePattern + '": ' + casesToDelete.size());
System.debug('');

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 5: PREVIEW LEADS
// ═══════════════════════════════════════════════════════════════════════════════

List<Lead> leadsToDelete = [
    SELECT Id, FirstName, LastName, Company, Status, CreatedDate
    FROM Lead
    WHERE LastName LIKE :namePattern
    OR Company LIKE :namePattern
    ORDER BY CreatedDate DESC
    LIMIT :maxRecords
];

System.debug('📋 LEADS matching "' + namePattern + '": ' + leadsToDelete.size());
System.debug('');

// ═══════════════════════════════════════════════════════════════════════════════
// SUMMARY
// ═══════════════════════════════════════════════════════════════════════════════

Integer totalRecords = accountsToDelete.size() + contactsToDelete.size() +
                       opportunitiesToDelete.size() + casesToDelete.size() +
                       leadsToDelete.size();

System.debug('═══════════════════════════════════════════════════════════════');
System.debug('SUMMARY - Records matching "' + namePattern + '"');
System.debug('═══════════════════════════════════════════════════════════════');
System.debug('  Accounts:      ' + accountsToDelete.size());
System.debug('  Contacts:      ' + contactsToDelete.size());
System.debug('  Opportunities: ' + opportunitiesToDelete.size());
System.debug('  Cases:         ' + casesToDelete.size());
System.debug('  Leads:         ' + leadsToDelete.size());
System.debug('  ─────────────────────────────────');
System.debug('  TOTAL:         ' + totalRecords);
System.debug('═══════════════════════════════════════════════════════════════');

// ═══════════════════════════════════════════════════════════════════════════════
// STEP 6: DELETE (if not preview mode)
// ═══════════════════════════════════════════════════════════════════════════════

if (!previewOnly && totalRecords > 0) {
    System.debug('');
    System.debug('⚠️  DELETING RECORDS...');

    // Delete in correct order (children before parents)
    Integer deletedCount = 0;

    // Delete Cases
    if (!casesToDelete.isEmpty()) {
        delete casesToDelete;
        deletedCount += casesToDelete.size();
        System.debug('  ✓ Deleted ' + casesToDelete.size() + ' Cases');
    }

    // Delete Opportunities
    if (!opportunitiesToDelete.isEmpty()) {
        delete opportunitiesToDelete;
        deletedCount += opportunitiesToDelete.size();
        System.debug('  ✓ Deleted ' + opportunitiesToDelete.size() + ' Opportunities');
    }

    // Delete Contacts
    if (!contactsToDelete.isEmpty()) {
        delete contactsToDelete;
        deletedCount += contactsToDelete.size();
        System.debug('  ✓ Deleted ' + contactsToDelete.size() + ' Contacts');
    }

    // Delete Leads
    if (!leadsToDelete.isEmpty()) {
        delete leadsToDelete;
        deletedCount += leadsToDelete.size();
        System.debug('  ✓ Deleted ' + leadsToDelete.size() + ' Leads');
    }

    // Delete Accounts (last - they're parents)
    if (!accountsToDelete.isEmpty()) {
        delete accountsToDelete;
        deletedCount += accountsToDelete.size();
        System.debug('  ✓ Deleted ' + accountsToDelete.size() + ' Accounts');
    }

    System.debug('');
    System.debug('═══════════════════════════════════════════════════════════════');
    System.debug('DELETE COMPLETE: ' + deletedCount + ' records moved to Recycle Bin');
    System.debug('═══════════════════════════════════════════════════════════════');
} else if (previewOnly) {
    System.debug('');
    System.debug('ℹ️  PREVIEW MODE - No records deleted');
    System.debug('   To delete, set previewOnly = false and run again');
}

// ═══════════════════════════════════════════════════════════════════════════════
// SF CLI ALTERNATIVE
// ═══════════════════════════════════════════════════════════════════════════════

/*
For large datasets, use sf CLI Bulk API:

# Step 1: Query and export IDs
sf data query \
  --query "SELECT Id FROM Account WHERE Name LIKE 'Test%'" \
  --target-org myorg \
  --result-format csv \
  > delete-accounts.csv

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

# For permanent delete (hard delete):
sf data delete bulk \
  --file delete-accounts.csv \
  --sobject Account \
  --target-org myorg \
  --hard-delete \
  --wait 30
*/
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall