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
Ashu sharma 38Ashu sharma 38 

how to empty recycle bin by apex

Hello,

How to empty the recycle bin by using apex.
As i am tring to 
    lead l=new lead(id='00Q7F00000CxIwa');
database.emptyRecycleBin(l);

it works,

but what will do if i want to empty all the records from different objects(As there are from differernt objecs how should i select particular object or should i write SOBJECT???)
Ajay K DubediAjay K Dubedi

Hi nitish,
Yes, you can pass all the list of Sobjects that you want to delete: 

Try using the below method:

Database.emptyRecycleBin(sObject[])
 


Hope it helps!  Mark it best if you find it helpful.

Thank you,
Ajay Dubedi

Akshay_DhimanAkshay_Dhiman
Hi Nitish,

Have a look at this code :
 
public class DeleteRecordsFromRecycleBin{

   public static void toDeleteSObjects(){
    
    //fetch the objects that are in recylce bin.   
    try{
     List<SObject>   accList=new list<account>([select id,name from account where isDeleted = true all rows]);
     system.debug(accList);

     List<SObject>   conList=new list<Contact>([select id,name from contact where isDeleted = true all rows]);
     system.debug(conlist);

     List<SObject>   oppList=new list<Opportunity>([select id,name from Opportunity where isDeleted = true all rows]);
     system.debug(oppList);
     //add all objects to a Sobject type list.
     List<SObject> listToDeleteRecycle=new List<Sobject>();
     listToDeleteRecycle.addAll(accList);
     listToDeleteRecycle.addAll(conList);
     listToDeleteRecycle.addAll(oppList);
  
     //delete the Sobject list from recycle bin.
     if(listToDeleteRecycle.size()>0)
      Database.emptyRecycleBin(listToDeleteRecycle);
     
    }
    catch(Exception e){
     system.debug(e.getmessage()+'At line number::'+e.getLineNumber());
    }
  }
}

 

Hope this code will help you resolve your query. 
If you find it helpful please mark it as best answer so that others can get help from this.

Thanks,
Akshay

 
RaghunadhRaghunadh
Working....Thanks