Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Restoring Deleted Records

After you have deleted records, the records are placed in the Recycle Bin for 15 days, after which they are permanently deleted. While the records are still in the Recycle Bin, you can restore them using the undelete operation. If you accidentally deleted some records that you want to keep, restore them from the Recycle Bin.

Example

The following example undeletes an account named 'Universal Containers'. The ALL ROWS keyword queries all rows for both top level and aggregate relationships, including deleted records and archived activities.
1Account a = new Account(Name='Universal Containers');
2insert(a);
3insert(new Contact(LastName='Carter',AccountId=a.Id));
4delete a;
5
6Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS]; 
7try {
8    undelete savedAccts;
9} catch (DmlException e) {
10    // Process exception here
11}

For more information on processing DmlExceptions, see Bulk DML Exception Handling.

Note

Undelete Considerations

Note the following when using the undelete statement.
  • You can undelete records that were deleted as the result of a merge. However, the merge reparents the child objects, and that reparenting can’t be undone.
  • To identify deleted records, including records deleted as a result of a merge, use the ALL ROWS parameters with a SOQL query.
  • See Referential Integrity When Deleting and Restoring Records.