[SalesForce] What’s the best way to delete lots of records via the API

I have a custom object set up and a scheduled task each night which needs to delete all the existing records and insert a load of new ones. It has been running fine for a while but now we have more data, so the initial SELECT which retrieves the Id's of the objects to be passed to the delete() API call is taking close to or over the 2 minute timeout. I thought it might be to do with the recycle bin so I have cleared them all out of there and adjusted the code to bypass the recycle bin, but the problem remains.

Currently the process is:

QueryResult sqr = SvcBinding.query("SELECT Id FROM MyCustomObj__c");
while (sqr.size > 0) {
    string[] ids = sqr.records.Select(b => b.Id).ToArray();
    DeleteResult[] dr = SvcBinding.delete(ids);

    // Empty deleted records from recycle bin
    string[] successIds = Array.FindAll(dr, res => res.success).Select(b => b.id).ToArray();
    SvcBinding.emptyRecycleBin(successIds);

    if (sqr.done)
        break;
    else
        sqr = SvcBinding.queryMore(sqr.queryLocator);
}

and then the new data is read from our internal DB and new MyCustomObj's inserted into SF via the same SOAP API.

Is there a better way of mass deleting the records, without having to select the Id's first, given that there is no filtering needed, just need to delete all records of MyCustomObj?

Best Answer

SalesForce themselves have suggested adding in ORDER BY Id as there are certain optimisations built in around the fact that Ids are ordered. The first run after making this change successfully avoided timing out, I will see how it runs over the next few days and mark this as an answer if it's helped.

Related Topic