如何在Salesforce中重置数据表?开发环境批量删记录方案咨询
Great question! Salesforce doesn’t have a direct equivalent to SQL’s TRUNCATE TABLE, but there are several far more efficient ways to wipe all records from your dev org than manual deletion. Here are the best approaches, including a dynamic method similar to what you’d do in SQL:
1. Dynamic Apex Batch Job (Your SQL-Like Approach)
This is the closest to generating a list of "tables" (objects) and deleting records en masse. You can write a reusable batch class that iterates over a list of object API names, queries all records in batches, and deletes them (with an option to skip the recycle bin via hard delete).
Sample Batch Class
global class BulkObjectDeleteBatch implements Database.Batchable<SObject> { private List<String> targetObjectApiNames; // Constructor to accept list of objects to process global BulkObjectDeleteBatch(List<String> objNames) { this.targetObjectApiNames = objNames; } global Database.QueryLocator start(Database.BatchableContext bc) { // Dynamically build a UNION ALL query to fetch IDs from all target objects String combinedQuery = 'SELECT Id FROM ' + String.join(targetObjectApiNames, ' UNION ALL SELECT Id FROM '); return Database.getQueryLocator(combinedQuery); } global void execute(Database.BatchableContext bc, List<SObject> records) { // Use hard delete (second parameter = true) to skip recycle bin (requires Bulk API enabled) Database.DeleteResult[] results = Database.delete(records, true); // Optional: Log failures for debugging for (Database.DeleteResult res : results) { if (!res.isSuccess()) { System.debug('Failed to delete record ID ' + res.getId() + ': ' + res.getErrors()[0].getMessage()); } } } global void finish(Database.BatchableContext bc) { System.debug('Bulk delete completed for objects: ' + targetObjectApiNames); } }
How to Run It
Execute this in the Developer Console or via Apex Anonymous:
// List of objects you want to wipe (custom or standard, as long as deletion is allowed) List<String> objectsToPurge = new List<String>{'Account', 'Contact', 'Custom_Inventory__c'}; // Run the batch with max batch size (2000) for optimal performance Database.executeBatch(new BulkObjectDeleteBatch(objectsToPurge), 2000);
2. Bulk Hard Delete via Data Loader
If you prefer no-code, use Salesforce Data Loader with the Hard Delete option (skips recycle bin, much faster than soft delete):
- Export all record IDs for each target object using a simple
SELECT Id FROM ObjectNamequery. - In Data Loader, select "Hard Delete" mode, upload the CSV of IDs, and run the job.
- Repeat for each object, or use the "Batch Processing" feature to handle multiple objects in one go.
3. Salesforce CLI Script (For DevOps/Automation)
For automated workflows, use the Salesforce CLI to loop through objects and delete records in bulk:
# List of objects to purge OBJECTS=("Account" "Contact" "Custom_Order__c") for obj in "${OBJECTS[@]}" do echo "Deleting records from $obj..." # Fetch all IDs and delete via Bulk API Hard Delete sf data query -q "SELECT Id FROM $obj" -r csv | tail -n +2 | sf data delete --sobject $obj --use-bulk-api-hard done
Critical Notes & Best Practices
- Check Dependencies: Master-detail relationships will auto-delete child records when parents are deleted, but lookup relationships may block deletion. Delete dependent records first if needed.
- Restricted Objects: Some standard objects (e.g.,
User,Profile,ApexClass) can’t be deleted or have strict limitations. - Permissions: Ensure your user has the
Modify All Datapermission to delete all records across objects. - Recycle Bin: Hard delete bypasses the recycle bin, so confirm you don’t need to recover these records later.
- Performance: Batch Apex and Bulk API handle large datasets (100k+ records) in hours, not weeks.
内容的提问来源于stack exchange,提问作者yoelbenyossef




