Sub topic 13
Verifying that an export is complete
An export that finishes without an error is not necessarily a complete export. Files can be truncated, a query can silently leave out rows, and records can change while the export runs. This chapter shows how to check an export before you rely on it as a backup.
It assumes you can run SOQL queries, use the Salesforce CLI (sf), and read CSV files. It also assumes you have already produced at least one export with Data Loader, the CLI, or the Data Export Service.
What "complete" means
An export is complete when three things are true:
- Row counts match. Every record the org held at the export's cutoff time appears in the file.
- The files are intact. Each file is readable, has the expected columns, and was not cut off.
- Relationships hold. Every lookup or master-detail Id in a child file points to a parent Id that exists in the export.
The third check matters most for restores, which are covered later in this tutorial. A restore that loads children before their parents fails on exactly the gaps this chapter looks for.
Fix a cutoff time first
Records keep changing while an export runs. If you compare an export that took 40 minutes against a count taken afterward, the numbers can differ for harmless reasons. To avoid this, choose a cutoff timestamp and use it in both the export query and the count query.
SELECT COUNT() FROM Account
WHERE CreatedDate < 2026-11-30T00:00:00ZThe export query then uses the same filter:
SELECT Id, Name, Industry, OwnerId, CreatedDate, LastModifiedDate
FROM Account
WHERE CreatedDate < 2026-11-30T00:00:00ZFilter on CreatedDate for a full export, because records are never created in the past. LastModifiedDate is the right filter for incremental exports, but a record edited during the run can legitimately fall on either side of the line.
Compare row counts
Get the expected number from the org with a count query:
sf data query \
--query "SELECT COUNT() FROM Account WHERE CreatedDate < 2026-11-30T00:00:00Z" \
--target-org my-prod-alias \
--jsonThe number is in result.totalSize in the JSON output. Then count the data rows in the CSV, which is the number of rows without the header.
Do not count with `wc -l`
Text fields such as descriptions and notes often contain line breaks. A CSV parser treats a quoted multi-line value as one row, but wc -l counts each physical line. The result is a number higher than the true row count, and you may wrongly conclude that the export has extra rows. Use a CSV parser instead:
import csv
import sys
def count_rows(path):
with open(path, newline="", encoding="utf-8-sig") as f:
reader = csv.reader(f)
next(reader) # skip the header row
return sum(1 for _ in reader)
if __name__ == "__main__":
print(count_rows(sys.argv[1]))Run it as python count_rows.py Account.csv. The utf-8-sig encoding strips the byte order mark that some tools add to the start of a CSV file.
Reasons counts differ
| Symptom | Likely cause |
|---|---|
| File has fewer rows than the org | The export hit a limit, the job was cut off, or a filter excluded records. |
| File has more rows than the org | You included deleted records with Export All or `queryAll`, but counted only live records. |
| Counts differ by a small, varying number | Records were created or deleted during the run, and no cutoff was used. |
| Counts differ by a large, fixed number | The running user cannot see some records. |
The last cause is a common one. A query returns only the records that the running user can see. Run exports as a user with a profile that can view all data, and do not assume a System Administrator profile alone is enough. Check the "View All Data" permission, and check object-level read access for each object.
Counting deleted records
If your export includes deleted rows, add IsDeleted to the field list. Count the live rows and the deleted rows separately, so each figure can be checked on its own. For deleted records, the Bulk API 2.0 queryAll operation returns them. For a job, the status call reports how much it processed:
curl "$INSTANCE_URL/services/data/v60.0/jobs/query/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"The JSON response includes state and numberRecordsProcessed. Accept the results only when state is JobComplete, and compare numberRecordsProcessed with your expected count and with the rows you actually downloaded. A mismatch between the last two points to an interrupted download.
Check file integrity
Row counts catch missing records. They do not catch a damaged file. Add these checks:
- Header check. Confirm the header row contains every field you asked for, in the expected names. A schema change, such as a deleted field, can alter an export between runs.
- Truncation check. Parse the whole file. A file cut off in the middle of a quoted value usually raises a parse error or yields a final row with too few columns.
- Size check. Compare the file size with the previous run. A drop of 50 percent without a matching drop in records deserves a look.
- Checksum. Store a SHA-256 hash next to each file when you create it. Recompute it when you copy or restore the file, to detect corruption in storage or transfer.
sha256sum Account.csv > Account.csv.sha256
sha256sum --check Account.csv.sha256On macOS, use shasum -a 256 in place of sha256sum.
For the Data Export Service, the output is a set of ZIP files that you download within a limited time. Download every ZIP part, extract them all, and check that there is one CSV for each object you expect. A missing part is easy to overlook, because each ZIP is valid on its own.
Check relationship Ids
Export the parent and the child, then confirm that every parent reference in the child file exists in the parent file. For Contacts and Accounts, that means every non-empty AccountId in the Contact file must appear as an Id in the Account file.
import csv
import sys
def read_column(path, column):
with open(path, newline="", encoding="utf-8-sig") as f:
return [row[column] for row in csv.DictReader(f)]
parent_ids = set(read_column(sys.argv[1], "Id"))
child_refs = [r for r in read_column(sys.argv[2], sys.argv[3]) if r]
orphans = [r for r in child_refs if r not in parent_ids]
print(f"{len(orphans)} of {len(child_refs)} references have no parent in the export")
sys.exit(1 if orphans else 0)Run it as python check_refs.py Account.csv Contact.csv AccountId. The script exits with status 1 when it finds orphans, so a scheduled job can fail loudly.
Orphans are not always an error. A child may point to a parent that was deleted, or to a parent excluded by your filter. Decide in advance which is acceptable. Also make sure both files use the same Id format. API exports return 18-character Ids. Some reports show 15-character Ids, which are case-sensitive and will not match the 18-character form in a plain string comparison.
Record the result
A check you run once and forget protects nothing. Write the results of each run, per object, to a small log: object name, cutoff time, expected count, exported count, checksum, and pass or fail. When a restore is needed months later, this log tells you which export to trust.
Exercise
An Account export file has 12,480 lines according to wc -l, but the org count query returns 11,950 records. The Description field contains line breaks. What should you do before concluding the export is wrong, and what other cause could explain a higher file count?
Show answer
Count the data rows with a CSV parser (for example Python's csv module), not wc -l, because quoted multi-line values are counted as several lines. If the parsed count is still higher than the org count, check whether the export included deleted records (Export All or queryAll) and whether the count query used the same cutoff time as the export.
Exercise
Your relationship check reports 40 Contacts whose AccountId is missing from the Account file. Name two possible causes and one way to tell them apart.
Show answer
The parent Accounts may have been deleted, or the Account export may have used a filter or a running user that excluded them. Query the org with queryAll (or look for IsDeleted = true rows) for those Ids. If they exist as deleted records, the cause is deletion. If they exist as live records, the Account export filter or user visibility is the cause.