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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone have any idea how to restore deleted records,normally deleted records store in recyclebin we can retrive from recycle even we deleted records from recyclebin how can we restore records using apex code?

share|improve this question
Is this still an open question? – Adrian Larson Mar 7 at 14:53

In every object there is one field ISDeleted when you delete records it get true. You need to set it false.

Update: Thanks @Ratan for suggestion. You can also try this method .


Account a = new Account(Name='Trump');
insert(new Contact(LastName='Carter',AccountId=a.Id));
delete a;

    Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Trump' ALL ROWS]; 
    try {
        undelete savedAccts;
    } catch (DmlException e) {
        // Process exception here
share|improve this answer
Hi @Tushar Sharma i tried this code but i m getting error "Field is not writeable: Account.IsDeleted ". – user123 Feb 2 at 5:57
@user123 i have also update another solution . You can check that too. – Tushar Sharma Feb 2 at 6:00
@user123 we use ALL ROWS to reterive record from recycle bin. Please check if your record exist in recycle bin – Tushar Sharma Feb 2 at 6:13
@user123 no you can't get them. – Tushar Sharma Feb 2 at 6:20

Check out how Using the Recycle Bin works (emphasis mine):

The Recycle Bin link in the sidebar lets you view and restore recently deleted records for 15 days before they are permanently deleted. Your Recycle Bin record limit is 25 times the Megabytes (MBs) in your storage. For example, if your organization has 1 GB of storage then your limit is 25 times 1000 MB or 25,000 records. If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours.

The same is true from Apex, you can only recover records for 15 days (unless you open a case with support). If you want to recover deleted records, you must undelete them as follows:

undelete [SELECT Id FROM Account WHERE IsDeleted = true ALL ROWS];

Note that records can be "hard deleted" even before the 15 day period is up. The main ways that this can happen are if someone calls Database.emptyRecycleBin, manually emptying the Recycle Bin through the UI, or using the Bulk API, which has a hard delete option.

share|improve this answer
doesn't using Data Loader Delete on lots of records also push aside (hard delete) older items in the recycle bin ? – cropredy Feb 3 at 2:03
@crop1645 If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours. I would think no based on the wording, since the operation woudl ostensibly complete within two hours. I have not used it much so I cannot confirm. – Adrian Larson Feb 3 at 2:05
i was thinking about the 15 day rule that isn't always the case as you pointed out; i know when I use Data Loader' delete, I get a warning message saying that the recycle bin may not be big enough to recover the records being deleted – cropredy Feb 3 at 2:07

Your Answer


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.