Deleting records from Recycle bin using Apex & Workbench

In Salesforce, the standard approach to delete the records from Recycle bin is to navigate to the Recycle bin, select the records that needs to be deleted, and click Delete to permanently delete them from the org. 

There are two other approaches to delete the records from the Recycle bin; the first approach is using Workbench. 

In Workbench, the Purge option is used to delete the recycle bin data from salesforce. Below are the steps using Purge option in Workbench to delete the records. 

  1. Log into the Workbench using the Salesforce organization credentials. 
  1. Select Purge Option 
  1. Compose a CSV file with the deleted record ids 

Page Break 

  1. Choose the file and click “Next” 
  1. Map the fields and click Confirm Purge button to Purge 

The records are purged, and result is shown below 

The second approach to delete the records from Recycle bin is using Apex. 

In Salesforce apex, the Database class has an emptyRecycleBin() method to permanently delete the specific records from the Recycle Bin. Below are the steps using Apex to delete the records. 

  1. Develop a class using emptyRecycleBin() method. 
  1. Below is the RecyclebinDelete apex class 

public class RecyclebinDelete { 

    public static void deleteRecords(){ 

        account a = new account(name = ‘test universal 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’); 

    } 

  1. Open the Execute anonymous window and use the code below and execute 

RecyclebinDelete.deleteRecords(); 

When the above code is executed, the class is invoked and the emptyRecycleBin() deletes the    record permanently from the Recycle Bin. 

  Below output shows the Debug log where an account record is created, deleted and undeleted.  

After undeleting, the record gets restored and is deleted again and the Database.emptyRecycleBin() method is invoked, and the record gets deleted permanently from the recycle bin; so, when the undelete DML is called, it throws the error “UNDELETE_FAILED, Entity is not in the recycle bin”. 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.