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
vasanth kumar 6vasanth kumar 6 

Need to truncate salesforce table (custom object) using apex

The table is huge and comprises more than 1 lakh records or may be 2 lakh..
The below apex code is of less help because of the limit set ..in our environment it is 10000...
delete [select id from <object_name> limit <limit#>]

Is there any other option to delete the records using some sort of iteration.. how to achieve?
Pls help..

Thanks
Kumar
 
vasanth kumar 6vasanth kumar 6
In some tech forums I did see mention of using map... kindly help whichever the easy and optimal way for deleting records which exceeds the governer limits set.. thanks! - Kumar
KaranrajKaranraj
Kumar - Write a Batch apex class to delete more records without hitting salesforce governor limits. Batch apex class is async process which will be executed based on the batch size you set while initating. 
  • In the start method of batch class get the records from the object using database.getquerylocator
  • In the excute method write the logic to delete the record. The excute method will process the record based on the batch size
  • Then in the finish method you can send email to the admin or any other person in your org which will be excuted only after processing all records.
Simple batch apex code
global class DeleteBulkRecords implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name FROM <ObjectName__c>';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<ObjectName__c> scope)
    {
      delete scope;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

Refer this link to understand more about batch apex class - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Thanks,
Karanraj (http://www.karanrajs.com)
KaranrajKaranraj
If you want to delete ALL records in your custom object then using Truncate object functioanlity you can do it without writing any code.Truncating custom objects is a fast way to permanently remove all of the records from a custom object, while keeping the object and its metadata intact for future use. Truncating is useful, for example, if you have created a custom object and filled it with test records. When you’re done with the test data, you can truncate the object to purge the test records, but keep the object and put it into production. This is much faster than batch-deleting records and possibly recreating the object.
  • From Setup, click Create | Objects.
  • Click an object name to go to the object’s detail page and click Truncate.
  • In the Confirm Custom Object Truncate window, review the warning and then enter the name of the object to truncate in the empty field.
  • Click Truncate.
Make sure to read this link before you are going to perform this action - https://help.salesforce.com/HTViewHelpDoc?id=dev_object_trunc_overview.htm&language=en_US#TruncatingObjectsOverview (https://help.salesforce.com/HTViewHelpDoc?id=dev_object_trunc_overview.htm&language=en_US#TruncatingObjectsOverview)