Deleting Records
After you persist records in the database, you can delete those records using the delete operation. Deleted records aren’t deleted permanently from Salesforce, but they are placed in the Recycle Bin for 15 days from where they can be restored. Restoring deleted records is covered in a later section.
Example
The following example deletes all accounts that are named 'DotCom':
Account[] doomedAccts = [SELECT Id, Name FROM Account
WHERE Name = 'DotCom'];
try {
delete doomedAccts;
} catch (DmlException e) {
// Process exception here
}
Referential Integrity When Deleting and Restoring Records
For example, if you delete a case record, Apex automatically deletes any CaseComment, CaseHistory, and CaseSolution records associated with that case. However, if a particular child record is not deletable or is currently being used, then the delete operation on the parent case record fails.
The undelete operation restores the record associations for the following types of relationships:- Parent accounts (as specified in the Parent Account field on an account)
- Indirect account-contact relationships (as specified on the Related Accounts related list on a contact or the Related Contacts related list on an account)
- Parent cases (as specified in the Parent Case field on a case)
- Master solutions for translated solutions (as specified in the Master Solution field on a solution)
- Managers of contacts (as specified in the Reports To field on a contact)
- Products related to assets (as specified in the Product field on an asset)
- Opportunities related to quotes (as specified in the Opportunity field on a quote)
- All custom lookup relationships
- Relationship group members on accounts and relationship groups, with some exceptions
- Tags
- An article's categories, publication state, and assignments