DML Undelete
The DML Undelete feature provides a powerful way to bulk undelete Salesforce records using SQL-like syntax.
This page covers only things specific to DML Undelete operation so please refer to DML Update page
to understand how DML operations work in general, how to execute them, and view the results.
DML Undelete Syntax
DML Undelete statements follow this syntax pattern:
UNDELETE [MAX <max_records>] [FROM] <SObject>
[USING SCOPE <scope>]
[WHERE <conditions>]
[ORDER BY <field> ASC|DESC]
[LIMIT <number>]
[OFFSET <number>]
Keep in Mind
- The optional
MAXclause acts as a safety mechanism to prevent accidentally undeleting too many records. If you specify that clause with a count, then the app will error the operation if the query matches more records than that count. - Only soft-deleted records (those in the Recycle Bin) can be undeleted - hard-deleted records cannot be recovered.
Undelete Scope
UNDELETE operations automatically search within deleted records only. You don't need to specify INCLUDE DELETED in
your WHERE clause - it's implied.
Basic Undelete Examples
Restore Recent Deletions
UNDELETE Account
WHERE IsDeleted = true
AND LastModifiedDate >= LAST_7_DAYS
Restore by Original Criteria
UNDELETE Contact
WHERE Account.Type = 'Customer'
AND Email LIKE '%@company.com'
Restore with Specific Conditions
UNDELETE Opportunity
WHERE StageName = 'Closed Won'
AND Amount > 10000
AND CloseDate >= THIS_QUARTER
Restore Related Records
-- First restore parent Account records
UNDELETE Account
WHERE Name LIKE '%Test Company%'
-- Then restore related Contact records
UNDELETE Contact
WHERE Account.Name LIKE '%Test Company%'
Conditional Restore with Date Ranges
UNDELETE Case
WHERE CreatedDate BETWEEN 2024-01-01 AND 2024-03-31
AND Priority = 'High'
AND Status = 'Closed'
Restore Using Complex Criteria
UNDELETE Lead
WHERE LeadSource IN ('Web', 'Email Campaign')
AND Rating = 'Hot'
AND CreatedDate >= LAST_MONTH
ORDER BY CreatedDate DESC
Related Features
- DML Delete: Understand what creates deleted records
- DML Update: Bulk update restored records
- Edit Data: Modify records after restoration