function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SimplySfdcSimplySfdc 

delete All records in an object

Hi, is there any easy ways to delete ALL records in an object with a single call rather than 200 records in batch.

sq
benjasikbenjasik
Not at this time.  For a custom object, you can delete the object definition which deletes all the data
SimplySfdcSimplySfdc
Hi Benji,
Thank you for the reply. Currently we have to do delete all records in a standard object. Total would be around 350K of records and it takes more than 2 until 3 hours. I have did bulk delete by 200. Do you have any ideas to make this process faster.  Thanks.

sq


Jon L. DaviesJon L. Davies

It should not take 2 to 3 hours to delete 350k of records.  Check the follow to see if you missed any steps.

  1. Keep your loop logic tight.  IE. Don't write out to the UI for every update as it slow down the loop
  2. Don't write to a log file until the end of the process. Disk hits slow the process.
  3. Read all IDs to be deleted into memory then preprocess the information.
  4. Don't read a file in on each loop. See 2 for reason.
  5. Turn on message compression

I can usually delete that many records in about 45 minutes give or take network bandwidth.  Number 1 above is the biggest issue I see with peoples code.  They have way to much going on during the delete loop.

JonD

360 Vantage
3115 S. Price Road
Chandler, AZ 85248

Mike LeachMike Leach

This C# code takes and ArrayList of sObject IDs (strings) and deletes them in bulk.

Code:

public int DeleteSObjects(ArrayList deleteList)
{
 int m_recordsProcessedCount = 0;

 SForceService svc = sForceService;
 if(deleteList == null || deleteList.Count == 0)
  return;

 if(deleteList.Count < 200)
 {
  string [] ids = new string[deleteList.Count];
  for(int i=0; i< deleteList.Count; i++)
  {
   ids[i] = (string)deleteList[i];
   m_recordsProcessedCount++;
  }
  
  svc.delete(ids);
 }
 else
 {
  int startIndex = 0;
  int endIndex = 200;
  
  while(endIndex <= deleteList.Count)
  {
   string[] batchIDs = new string[endIndex - startIndex];
   int batchIndex = 0;
   for(int i=startIndex; i < endIndex; i++)
    batchIDs[batchIndex++] = (string)deleteList[i];
   
   DeleteResult [] dr = svc.delete(batchIDs);
   
   for(int j=0; j< dr.Length; j++)
   {
    if(dr[j].success){m_recordsProcessedCount++;}
    else
    {
     foreach(Error e in dr[j].errors)
      Console.WriteLine(e.message); //Or log and print later
    }
   }
   
   if(endIndex >= deleteList.Count)
    break;
   
   startIndex = endIndex;
   endIndex += 200;
   if(endIndex > deleteList.Count)
    endIndex = deleteList.Count;
  }
 }
 return m_recordsProcessedCount;
}