8
Delete | Apex DML Standalone Statement
Chapter Topics
- What Is DML In Salesforce?
- Insert | APEX DML Standalone Statements
- Update | APEX DML Standalone Statements
- Upsert | APEX DML Standalone Statements
- Delete | Apex DML Standalone Statement
- Undelete | Apex DML Standalone Statement
- Merge | APEX DML Standalone Statements
- APEX DML Statements Best Practices
- Database Class in APEX
- Empty Recycle Bin
- Count Query | Method in Apex Database Class
- Lead Conversion | Method in Apex Database Class
- Transaction Control and Rollback
- Database Class Method Result Object
Delete In Apex
To delete a record, you need to use the Delete statement.
Example
List < Contact > conList = [SELECT Name FROM Contact];
Map < String, ID > mMap = new Map < String, ID > ();
for (Contact c: conList) {
mMap.put(c.Name, c.Id);
}
List < Contact > uniqList = new List < Contact > ();
List < Contact > delList = new List < Contact > ();
Set < String > sSet = mMap.keySet();
Set < ID > uniqSet = new Set < ID > ();
for (String s: sSet) {
uniqSet.add(mMap.get(s));
}
for (Contact c1: conList) {
if (uniqSet.contains(c1.Id)) uniqList.add(c1);
else delList.add(c1);
}
delete delList;
Note:
- Use is Deleted=true & ALL ROWS keyword to get the deleted records from the recycle bin using SOQL.
- ALL ROWS keyword cannot be used in the query editor but can be used in the apex class or function while querying record in [].

Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.