Site icon WedgeCommerce

Remove data from recycle bin in APEX

DML are some of the basic operations which are required by any developer who is working on a database. Same is the case with Salesforce, and DML are some of the basic operations that Salesforce developers require. We have insert, update, upsert, delete, undelete and merge operations. We all know this too that Salesforce also has it’s own recycle bin. Whenever data is deleted it goes to recycle bin. Now for APEX developers, this question arises, is it possible to completely purge the data? Would you like it if I said that yes, this is possible? This is the topic of discussion for this blog, can we remove data from recycle bin in APEX?

Apex Code

Let’s jump to the main course, how will it be done? Well use the code below and create a class in Salesforce.

public class PurgeTest {
    public static void CreateAndPurge(){
        account a = new account(name = 'test acc');
        insert a;
        system.debug(a);
        delete a;
        try{
            account abc = [select id from account where id=:a.id];
        }catch(exception e){
            system.debug('Account Not Available');
        }
        undelete a;
        try{
            account abc = [select id from account where id=:a.id];
            system.debug(abc);
        }catch(exception e){
            system.debug('Account Not Available');
        }
        delete a;
        database.emptyRecycleBin(a);
        system.debug(a);
        try{
            undelete a;
        }catch(exception e){
            system.debug(e.getMessage());
        }
        system.debug('end');
    }
}

After you paste this code, go to the Execute anonymous window and write this line, and execute.

purgeTest.CreateAndPurge();

The function emptyRecycleBin() is doing the main work over here for deleting the records completely.

Output

The output of the following code looks something like this.

Support

That’s all about removing data from recycle bin in Salesforce, for any further queries feel free to contact us at:

https://wedgecommerce.com/contact-us/

Or let us know your views about this blog in the comments section below.

Exit mobile version