Search code examples
c#salesforce

How can i delete a large number of records in Salesforce using Apex code?


I have over 5000 Account records in Salesforce that need to be deleted.

I am currently using the following in my c# code

  SalesForceEnterpriseService.DeleteResult[] delResult = _service.delete(ids); 

and ids is a string array defined as follows: string[] ids. When the above code runs , it gives an error: EXCEEDED_ID_LIMIT: delete id limit reached: 200. Is there a way i can delete these 5000 records calling my service from within c# ?


Solution

  • Building Apex REST (or SOAP) service just for this sounds like overkill, the overhead of writing unit test, deploying... not worth it.

    From C# - can you call that delete in a loop, 200 records at a time? It'll be bit more network traffic but should be straightforward.

    From Apex - do you know how to run "execute anonymous"? delete [SELECT Id FROM Account WHERE Id IN ('001...', '001...'); should be good start, add LIMIT 1000 if the operation times out.

    If you routinely need to process large amounts of data - consider using Bulk API. But it's REST (your thing looks like it uses SOAP) and it works bit different. You submit a job (csv/xml/json), start it and periodically poll "is it done yet". Whereas your SOAP call just takes that 200 and waits till server returns (synchronous vs asynchronous)

    Check https://trailhead.salesforce.com/content/learn/modules/api_basics/api_basics_bulk and https://trailhead.salesforce.com/content/learn/modules/large-data-volumes/load-your-data if you're curious.