+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

how to empty recycle bin by apex


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

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:


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

Thank you,
Ajay Dubedi

Hi Nitish,

Have a look at this code :
public class DeleteRecordsFromRecycleBin{

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

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

     List<SObject>   oppList=new list<Opportunity>([select id,name from Opportunity where isDeleted = true all rows]);
     //add all objects to a Sobject type list.
     List<SObject> listToDeleteRecycle=new List<Sobject>();
     //delete the Sobject list from recycle bin.
    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.