Sub topic 7
Including deleted records
This chapter assumes you can write a basic SOQL query, you know how to run Data Loader or the Salesforce CLI, and you have read the earlier chapters on choosing an export method. By the end you will know why a normal export misses deleted records, how to include them, and what no export can recover.
Why a normal query misses deleted records
When a user deletes a record in Salesforce, the platform does a soft delete. The record keeps its Id and field values, but its IsDeleted field becomes true and it moves to the Recycle Bin. A standard SOQL query filters those rows out, so an export built on a plain SELECT never sees them.
A soft-deleted record stays recoverable for a limited time. After that, or after someone empties the Recycle Bin, the record is purged and cannot be queried at all.
What counts as "deleted" for an export
Several different situations leave a record with IsDeleted = true or hidden from a normal query:
- Soft-deleted records. These are in the Recycle Bin. They are the main target of this chapter.
- Cascade-deleted children. Deleting an Account also deletes its Contacts, Opportunities, and other children that are in a master-detail or similar cascading relationship. Each child appears as its own deleted row.
- Merged records. When you merge duplicates, the losing records are deleted. Their
MasterRecordIdfield points to the surviving record, which helps you trace references to old Ids. - Archived activities. Old Tasks and Events can be archived. Like deleted rows, they are hidden from a normal query.
Records that were hard deleted or purged are none of these. They are gone from the org, so only an earlier backup can bring them back.
Querying deleted records
You add deleted rows by changing how the query is run, not only the SOQL text. The API operation is called queryAll. In Apex, the equivalent is the ALL ROWS keyword. You usually also select IsDeleted so you can tell the two groups apart in the output.
This query lists only deleted Accounts. Run it with queryAll, because a plain query returns zero rows for it.
SELECT Id, Name, IsDeleted, SystemModstamp
FROM Account
WHERE IsDeleted = true
ORDER BY SystemModstampDeleting a record updates its SystemModstamp, so you can filter on it to find recent deletions. This is how you build an incremental export of deletions.
SELECT Id, Name, IsDeleted, MasterRecordId, SystemModstamp
FROM Account
WHERE IsDeleted = true
AND SystemModstamp >= 2026-11-01T00:00:00ZIf you drop the WHERE IsDeleted clause and still run queryAll, you get active and deleted rows together. That is the usual choice for a full backup, as long as IsDeleted is one of the exported columns.
Choosing the tool
| Tool | How to include deleted rows | Notes |
|---|---|---|
| Data Loader | Use the **Export All** operation instead of **Export** | Same SOQL editor and CSV output |
| Bulk API 2.0 | Set `"operation":"queryAll"` when creating the query job | Suited to large volumes |
| Salesforce CLI | Check `sf data query --help` for an option that includes deleted rows in your CLI version | Without it, deleted rows are not returned |
| Apex | Add `ALL ROWS` to the SOQL statement | Only useful for in-org processing |
| Data Export Service | Not configurable | Do not rely on it as your only source of deleted data |
In Data Loader, pick Export All, choose the object, and write the query. Tick the IsDeleted field in the field list so the column appears in the CSV.
For Bulk API 2.0, the job request is the same as in the earlier chapter, with one changed value:
curl -X POST "$INSTANCE_URL/services/data/v60.0/jobs/query" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"operation":"queryAll","query":"SELECT Id, Name, IsDeleted, SystemModstamp FROM Account","contentType":"CSV"}'Polling the job and downloading the results work exactly as for a normal query job.
Reading the output
Once IsDeleted is in the file, every row carries true or false. A few habits make the file safe to use:
- Keep deleted and active rows in the same file for a full backup, so one record count covers both.
- Never load the file back unchanged. Loading a deleted row as a new insert would create an active record that someone intentionally removed.
- Filter on
IsDeletedbefore any restore, and decide row by row whether a deleted record should come back.
Restoring a deleted record that is still in the Recycle Bin is easier with undelete than with a reload. You can undelete from the Recycle Bin page or with the Apex undelete statement, and the record returns with the same Id and its cascade-deleted children. Reloading from CSV gives the record a new Id and breaks any lookups that pointed to the old one.
Common errors
- The query returns zero rows for
IsDeleted = true. You used a normal query or Data Loader Export. Switch toqueryAllor Export All. IsDeletedis missing from the CSV. You did not select the field. Add it to theSELECTlist.- Counts do not match the UI. A count taken with a plain
COUNT()excludes deleted rows. AqueryAllexport includes them. Compare like with like, as covered in the verification chapter. - A deleted record you expected is absent. It was probably purged after the retention window, emptied from the Recycle Bin, or hard deleted. The export cannot return it.
- Child rows appear deleted when you only deleted the parent. This is the cascade delete working as designed.
Limits to plan around
Deleted data is only one part of the picture. A queryAll export has the same API limits as any other query, and the extra rows add to your volume. A few points apply here:
- Soft-deleted records still count toward data storage until they are purged.
- A hard delete skips the Recycle Bin, so a backup taken before the delete is the only source.
- Archived activities are returned by
queryAll, which can increase the size of Task and Event exports noticeably.
Exercise
A nightly Data Loader job runs Export on Contact and writes a CSV. A user deletes 40 Contacts on Tuesday afternoon, and the Recycle Bin is emptied on Wednesday morning. Can you recover those Contacts from Tuesday night's file, and what would you change?
Show answer
No. The Tuesday night file came from a normal export, so it excludes soft-deleted rows, and the records were purged on Wednesday, so no query can return them. Change the job to Export All (or a Bulk API 2.0 queryAll job) and select the IsDeleted field. Then the Tuesday night file contains the 40 Contacts with IsDeleted set to true, and you can restore them from the file. For records still in the Recycle Bin, use undelete instead of reloading.
Exercise
You run SELECT COUNT() FROM Account in the UI and get 12,400. Your queryAll export of Account has 12,650 data rows. Give a likely explanation and a query that confirms it.
Show answer
The export includes about 250 deleted Accounts that the plain count excludes. Confirm it with SELECT COUNT() FROM Account WHERE IsDeleted = true, run with queryAll. If the result is 250, the two numbers agree.