---
schema_version: "1"
id: 07e0d9f9e9d07ac4b7b3cbd2729fe92b2d27fa4ddb55756dbba0c4634ddf8704
title: Restoring Deleted Records
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_dml_examples_undelete.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_undelete.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: f1867f5faad28f34fc17a7d1aceaab38c3be9c6179ce19e248f1ca2e0ea23fef
---
# 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.

Account a = new Account(Name='Universal Containers');
insert as user a;
insert as user new Contact(LastName='Carter',AccountId=a.Id);
delete as user a;

Account\[\] savedAccts = \[SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' WITH USER\_MODE ALL ROWS\]; 
try {
    undelete as user savedAccts;
} catch (DmlException e) {
    // Process exception here
}

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

For more information on processing DmlExceptions, see [Bulk DML Exception Handling](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_dml_bulk_exceptions.htm).

## 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](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_delete.htm#referential_integrity).

#### See Also

- [Querying All Records with a SOQL Statement](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm)