Every Salesforce file migration starts with the same question: how many files are we dealing with? The answer drives scope, timeline, cost, and tool choice. But Salesforce makes counting harder than it should be — files live across four different objects, and a naive query can double-count or miss them entirely. This guide gives you the SOQL you actually need, explains what each query is counting (and why), and walks through a worked example for scoping a real migration.
Why count files before you migrate?
Three reasons. First, scope: a 5,000-file migration is a one-day project and a 500,000-file migration is a six-week project. Second, cost: Salesforce file storage is priced per GB, and the bigger the file footprint the bigger the bill — both at the source (where you may be paying for storage you'll abandon) and at the target (where you need capacity to receive it). Third, tooling: under 1,000 files you can probably do it manually; above that you need real tooling with progress tracking, retry logic, and per-file reporting.
Counting also surfaces surprises. The legacy Attachment count is usually a multiple of what people assume, because Attachments aren't visible in the modern Files UI but accumulate quietly on records.
The three file types in Salesforce
Files in Salesforce live in three places:
- Modern Files —
ContentDocument+ContentVersion+ContentDocumentLink. The current model. Visible in the "Files" related list, in Files Home, and in the Notes & Attachments list (yes, both). - Legacy Attachments —
Attachmentobjects. The older model, superseded by Files. Visible only in the "Notes & Attachments" related list. Many orgs have lots of these and don't realize it. - Notes — both legacy
Noteobjects and modernContentNoterecords. Notes contain text content but they're stored alongside files in some object models, so a thorough inventory includes them.
Migration tools handle each type differently. The tooling section of our ContentVersion vs. Attachment guide covers the differences in detail.
Counting ContentDocuments and ContentVersions
First, run every query in this guide as a user with View All Data and the Query All Files permission. Without it, file queries skip any file the user can see only through a record, even with View All Data, and your counts come out low.
The simplest count — total ContentDocuments in the org:
SELECT COUNT() FROM ContentDocument
That gives you the number of distinct files. Each ContentDocument has at least one ContentVersion, but may have many (one per uploaded version). To count just the latest version of each file:
SELECT COUNT() FROM ContentVersion WHERE IsLatest = true
That number should match the ContentDocument count. If it doesn't, check file visibility first (the permission above), then archived documents: query() skips them and queryAll() returns them.
To count all versions including history:
SELECT COUNT() FROM ContentVersion
The difference between the two ContentVersion counts tells you the version-history overhead in your storage.
Counting legacy Attachments
The Attachment object is dead simple — one row per file:
SELECT COUNT() FROM Attachment
One catch: the query doesn't return attachments on emails unless the user has Modify All Data.
To filter by parent object type, use the ParentId prefix. Account IDs start with 001, Contacts with 003, Cases with 500, Opportunities with 006 (Salesforce's Entity Key Prefix Decoder lists the rest):
SELECT COUNT() FROM Attachment WHERE Parent.Type = 'Account'
Or to get a parent-type breakdown in one query:
SELECT Parent.Type, COUNT(Id)
FROM Attachment
GROUP BY Parent.Type
ORDER BY COUNT(Id) DESC
Counting Notes
Both objects, separately:
SELECT COUNT() FROM Note
SELECT COUNT() FROM ContentNote
The legacy Note object is the older model; ContentNote is the modern one (it's built on ContentVersion, so it's already counted in your ContentDocument total above). For a true unique count, query Note separately and add it to the ContentDocument count.
Per-object breakdowns
ContentDocumentLink is the join table connecting ContentDocuments to parent records. Salesforce rejects a query on it without a filter unless the running user has Query All Files (API version 59.0 and later), which is one more reason to set that permission up first. To count modern files per parent object:
SELECT LinkedEntity.Type, COUNT(Id)
FROM ContentDocumentLink
GROUP BY LinkedEntity.Type
ORDER BY COUNT(Id) DESC
Note that ContentDocumentLink can have multiple rows per ContentDocument (a file shared with three Accounts has three ContentDocumentLink rows). The count above is link count, not unique file count. To dedupe, you need distinct ContentDocumentId values. SOQL has COUNT_DISTINCT(), but for an org-wide unique count by object it's usually simpler to export the links and de-duplicate ContentDocumentId outside Salesforce.
For a more complete parent-object breakdown, the Salesforce Attachment Counter handles dedup automatically — and includes custom objects without needing you to know their key prefixes in advance.
Counting by file size
Counts alone don't tell you what your storage looks like. To total file size on modern files, include every version, because old versions use storage too:
SELECT SUM(ContentSize) FROM ContentVersion
That returns total bytes. Divide by 1024 * 1024 * 1024 for GB. ContentSize covers files under 2 GB; for larger files use ContentSizeLong (API version 66.0 and later). Add WHERE IsLatest = true if you only want the size of what a migration would copy.
For Attachments and Documents:
SELECT SUM(BodyLength) FROM Attachment
SELECT SUM(BodyLength) FROM Document
Together those numbers tell you your total file storage footprint (it's the same calculation Salesforce uses in its own file storage article) — useful for both migration planning and storage cost calculation. See our storage cost reduction guide for what to do with that number.
Worked example: scoping a 50K-file org
Imagine you run the queries above and find:
| Type | Count | Total size |
|---|---|---|
| ContentDocument | 32,400 | 78 GB |
| Attachment | 17,200 | 22 GB |
| Note (legacy) | 8,100 | 12 MB |
| Total files and attachments | 49,600 | 100 GB |
And the per-object breakdown:
| Parent object | ContentDocuments | Attachments | Total |
|---|---|---|---|
| Case | 14,800 | 9,200 | 24,000 |
| Account | 8,600 | 4,400 | 13,000 |
| Opportunity | 5,200 | 2,100 | 7,300 |
| Custom_Project__c | 3,800 | 1,500 | 5,300 |
Several scoping decisions fall out:
- The Case-related files alone are 24,000 — the migration's largest single object scope. Plan it as a phase by itself.
- Legacy
Attachments are 35% of files and 22% of storage. Decide up front: migrate them asAttachments in the target, convert them toContentDocuments in flight, or leave them behind. - 100 GB of storage is non-trivial. Verify the target org's file storage limit (Salesforce's published allocation is 10 GB per org, or 1 GB on Essentials). May require purchased storage or pre-migration archival.
- The custom object
Custom_Project__chas 5,300 files. Confirm it exists in the target org with matching field schema before migration starts.
Gotchas and edge cases
- Deleted and archived files — files in the Recycle Bin and archived documents are excluded from default queries. To include them, use
ALL ROWSin Apex or the REST API's QueryAll resource. - Files attached to deleted parents —
ContentDocumentLinkrows can outlive their parent. Filter onLinkedEntityIdvalidity if cleanliness matters. - Email attachments — outbound and inbound emails store attachments differently (sometimes as
EmailMessage.Attachments, sometimes asAttachment, sometimes asContentDocument). Worth a separate count if your org uses Email-to-Case heavily. - Sandbox vs production drift — Partial Copy sandboxes hold only a sample of production data, so they don't include all files. Don't count in sandbox and assume the number applies to production.
Need a precise inventory without writing all this SOQL?
The Salesforce Attachment Counter runs the queries above (and several you didn't know you needed) with live progress, full job history, and email notification — including custom objects with no manual configuration.
Sources
- ContentVersion · Salesforce Object Reference
- ContentDocumentLink · Salesforce Object Reference
- Attachment · Salesforce Object Reference
- ContentNote · Salesforce Object Reference
- Aggregate Functions · SOQL and SOSL Reference
- Salesforce Entity Key Prefix Decoder · Salesforce Help
- Salesforce File Storage Limit – Understanding and Reducing File Storage Usage · Salesforce Help
- Data and File Storage Allocations · Salesforce Help