DML Delete
The DML Delete feature provides a powerful way to bulk delete Salesforce records using SQL-like syntax. It supports both soft deletes (which can be undeleted later) and hard deletes (permanent removal). This feature includes comprehensive safety mechanisms to prevent accidental data loss.
This page covers only things specific to DML Delete
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 Delete Syntax
DML Delete statements follow this syntax pattern:
DELETE [HARD] 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 critical safety mechanism to prevent accidentally deleting too many
records.
Delete Types
Soft Delete (Default)
DELETE MAX 50 Account
WHERE Type = 'Prospect' AND CreatedDate < LAST_YEAR
Soft delete moves records to the Recycle Bin where they can be restored. This is the default and recommended approach for most scenarios.
While Soft Delete moves the records to the Recycle Bin, it is not guaranteed to be recoverable. It depends on how many records you’re deleting, type of object etc., So it is always a good idea to back up critical data before performing deletes.
Hard Delete
DELETE HARD MAX 25 Test_Data__c
WHERE CreatedDate < LAST_MONTH
Hard deleted records CANNOT be recovered! Make sure you have a back up of the data as necessary.
Delete Examples
Delete by Status
DELETE MAX 100 Lead
WHERE Status = 'Unqualified'
AND CreatedDate < LAST_90_DAYS
Delete with Relationship Criteria
DELETE MAX 50 Contact
WHERE Account.Type = 'Prospect'
AND LastActivityDate < LAST_6_MONTHS
Delete Using Complex Conditions
DELETE MAX 200 Case
WHERE Status = 'Closed'
AND CreatedDate < LAST_YEAR
AND Priority IN ('Low', 'Medium')
Related Features
- DML Update: Bulk update records
- DML Undelete: Restore soft-deleted records
- Edit Data: Interactive record editing
- Export Records: Backup data before deletion
- Recycle Bin Management: Salesforce standard recovery tools