We have managed package and to test this we have special Dev & QA org from sales force which higher data limits (one of them is 250 mb org).

To test various scenarios we need to create may different data set. We have build tool for this data generation.

But to re try verity of scenario we need to delete data data and insert again new set of data.

Deleting data is taking lot of manual effort. First export all data then call delete api with id. How can i delete all data with minimum manual effort.

2 Answers 2


It would be pretty straightforward to write a minimum viable batch to clear one table at a time for this scenario:

public class DeleteAllBatch implements Database.Batchable<SObject>
    final SObjectType deleteType;
    public DeleteAllBatch(SObjectType deleteType) { this.deleteType = deleteType; }

    public Database.QueryLocator start(Database.BatchableContext context)
        return Database.getQueryLocator('SELECT Id FROM ' + deleteType);
    public void execute(Database.BatchableContext context, List<SObject> scope)
        Database.delete(scope, /*allOrNone*/ false);
    public void finish(Database.BatchableContext context) { }

You could extend it to list out multiple types with some minor modifications.

public class DeleteAllBatch implements Database.Batchable<SObject>
    final SObjectType deleteType;
    final List<SObjectType> deleteTypes;
    public DeleteAllBatch(List<SObjectType> deleteTypes)
        this.deleteType = deleteTypes.remove(0);
        this.deleteTypes = deleteTypes;

    public Database.QueryLocator start(Database.BatchableContext context) { /*same*/ }
    public void execute(Database.BatchableContext context, List<SObject> scope) { /*same*/ }
    public void finish(Database.BatchableContext context)
        if (!deleteTypes.isEmpty())
            Database.executeBatch(new DeleteAllBatch(deleteTypes));
  • But how to detect dynamically the correct sequence in which objects should be removed, e.g. remove child object first and parent afterwards?
    – wesaw
    Commented Aug 14, 2017 at 18:03

You could use the 'Mass Delete Records' in data management. I have not actually tried out this option hence I am unaware how effective this is.

Alternatively, you could keep apex script ready that can be executed in the developer console. The script will query all records of each object and delete them. This method could be reused whenever required. The only caveat is that you will need to maintain the list of objects whose records you need to delete, in your case all the objects that have records. This is basically a one time activity and you just need to keep updating the list whenever you have a new object under which you have records. You will have to add custom settings too if you interested in deleting them too.

Execute the apex script in developer console execute anonymous. Voila! You have a clean org.

//Suppose your org has only account, contact and lead data
//You will need to maintain the below list
//Add objects to it every time you have a new object
//Compliling it the first time may be a little cumbersome, het BTW reusabile and efficient+fast anyday!!!!
//Keep the list size below 150, to avoid hitting 151 DML governor limit - do it in multiple batches if required
List<String> ObjectList = new List<String>{'Account','Contact','Lead'}; //API NAMES of all objects whose records you want to delete
String QueryConstantString = 'SELECT id FROM ';

for(String obj : ObjectList){
    String queryString = QueryConstantString + obj;
    List<sobject> SObjectList = database.query(queryString);
    delete SObjectList;

I haven't really tried out the above code.

PS : Since it is a dev org not expecting many records so as to hit 50000 query rows gov limit. If it happens, need to split it in batches.

  • 1
    Using a batch, credits @adrianLarson will give you higher governor limits - actually more suited to deleting huge chunks of data kinda operations. I believe that we could combine above method and make it a batch. Thanks
    – Prajith
    Commented May 4, 2016 at 21:38
  • 2
    One other thing-- In a list of multiple object types, like this one, put accounts last. The reason why is that accounts won't delete if they have open cases or opportunities, so you end up having to run over them twice otherwise.
    – sfdcfox
    Commented May 4, 2016 at 23:00
  • 1
    You also have to be aware of restricted delete relationships that will dictate order of sobject deletion
    – cropredy
    Commented May 5, 2016 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .