• #30715

    How can we hard delete a record using a Salesforce Apex class by code?

    How can we hard delete a record using a Salesforce Apex class by code?



    Hi Madhulika,

    Hard delete is done by using DataBase.emptyRecycleBin method in the Batch class. Create a sample Batch class as mentioned below and use DataBase.emptyRecycleBin method in the Batch class.

    global class BatchDeletion implements Database.Batchable<sObject>, Schedulable
    global BatchDeletion()

    global Database.QueryLocator start(Database.BatchableContext bc)
    //query to return all expired Case Share records
    return Database.getQueryLocator([Select id from Account where Name=’Test Account12′]);

    global void execute(SchedulableContext sc)
    //execute the batch
    BatchDeletion deleteCS = new BatchDeletion();
    ID batchprocessid = Database.executeBatch(deleteCS);

    global void execute(Database.BatchableContext BC, list<sObject> scope)
    System.debug(‘## deleting ‘+scope.size()+’ case share recs’);

    //delete list of expired Case Share records
    delete scope;

    global void finish(Database.BatchableContext BC)
    //no post processing
    /* System.debug(‘## Batch Job Finished ##’);
    UpdateAccountFields m = new UpdateAccountFields ();
    String sch = ’20 30 8 10 2 ?’;
    system.schedule(‘Merge Job’, sch, m);*/


    Hope this will help you.


    Salesforce | shariq shariq #1


    We can hard delete record or list of records using emptyRecycleBin() function in apex. Pass the record or record list to emptyRecycleBin() to delete it from Recycle Bin.
    Contact con = new Contact(Id = ’09k110000O5abc’);

    Note: The DML operation datatbase.emptyRecycleBin() is limited to 200 items.

    Hope this helps.

Viewing 3 posts - 1 through 3 (of 3 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?