Skip to main content

DML Undelete

Professional

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>]

The MAX clause is required and acts as a safety mechanism to control how many records are restored in a single operation. 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 MAX 50 Account
WHERE IsDeleted = true
AND LastModifiedDate >= LAST_7_DAYS

Restore by Original Criteria

UNDELETE MAX 100 Contact
WHERE Account.Type = 'Customer'
AND Email LIKE '%@company.com'

Restore with Specific Conditions

UNDELETE MAX 25 Opportunity
WHERE StageName = 'Closed Won'
AND Amount > 10000
AND CloseDate >= THIS_QUARTER

Restore Related Records

-- First restore parent Account records
UNDELETE MAX 10 Account
WHERE Name LIKE '%Test Company%'

-- Then restore related Contact records
UNDELETE MAX 50 Contact
WHERE Account.Name LIKE '%Test Company%'

Conditional Restore with Date Ranges

UNDELETE MAX 200 Case
WHERE CreatedDate BETWEEN 2024-01-01 AND 2024-03-31
AND Priority = 'High'
AND Status = 'Closed'

Restore Using Complex Criteria

UNDELETE MAX 100 Lead
WHERE LeadSource IN ('Web', 'Email Campaign')
AND Rating = 'Hot'
AND CreatedDate >= LAST_MONTH
ORDER BY CreatedDate DESC