Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I understand that when I perform a Database.delete() operation, records will end up in my org's recycle bin. And their presence in the recycle bin is reflected by the IsDeleted field on the object.

But if those records are (for example) scrapes of a web page, they are of little interest to users and I don't want them kicking around in the org. Currently the only way I can see to 'hide' them would be to misuse a Protected (List) Custom Setting object.

From Apex, how can I delete a record and skip this, so it doesn't clutter a user's recycle bin?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Take a look at the Database.emptyRecycleBin(sObject) method or the other variants in the Database class such as Database.emptyRecycleBin(sObject[])

For example, to permanently delete a contact with Id of '003i000000O4xYZ' in the recycle bin:

Contact c = new Contact(Id = '003i000000O4XyZ');
Database.emptyRecycleBin(c);
share|improve this answer
1  
Just nipped me to the post on this one Peter! ;-) Also note that there is a couple of other variants of this method. One which takes a list of Id's as well. –  Andrew Fawcett Dec 23 '13 at 13:34
1  
Good point. Updated the answer. Being on xmas vacation I don't have my actual work to get in the way of important things like tending to the SFSE. ;) –  Peter Knolle Dec 23 '13 at 13:39
    
Thanks @PeterKnolle - missed that! –  user320 Dec 26 '13 at 16:42
    
Thanks a lot for the answer, Peter Knolle, it was quite useful. I think the links in the answer are not working any more, though, I am receiving a 404 Not Found error. I guess it was referencing Apex Developer's Guide, so it was quite easy to find the related entry there. –  Jesus Arcas FinancialForce Jun 24 at 8:07
    
@JesusArcasFinancialForce - Thanks. I updated the answer with fixed links. –  Peter Knolle Jun 24 at 14:16

It is possible to hard delete 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, Schedulable {

global BatchDeletion()
{
       //constuctor  
}


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;   
        DataBase.emptyRecycleBin(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);*/

}

}

Note: 1. The DML operation datatbase.emptyRecycleBin is limited to 200 items, it is a known issue and currently no workaround is possible for this. http://success.salesforce.com/issues_view?id=a1p30000000STLXAA4

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.