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. This is useful, for example, if you accidentally deleted some records that you want to keep.

Example

The following example undeletes an account named 'Trump'. The ALL ROWS keyword queries all rows for both top level and aggregate relationships, including deleted records and archived activities.
Account a = new Account(Name='Trump');
insert(a);
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
}
Note

Note

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

Undelete Considerations

Note the following when using the undelete statement.
  • You can undelete records that were deleted as the result of a merge, but the child objects will have been reparented, which cannot be undone.
  • Use the ALL ROWS parameters with a SOQL query to identify deleted records, including records deleted as a result of a merge.
  • See Referential Integrity When Deleting and Restoring Records.
Previous
Next