Salesforce Files Data Structure Explained: ContentDocument, ContentVersion and ContentDocumentLink

Upload one PDF to an account and Salesforce creates at least three records in three objects, plus a fourth if you share it anywhere else. That design is why a file can appear on several records without being duplicated and why it keeps a version history. It is also why "just query the files" is harder than it sounds, and why exports, migrations and clean-ups go wrong when the model is misunderstood.

This is a reference for admins and developers who need to find, count, copy, move or export Salesforce Files and want to know what is happening underneath.

The Three Objects

ObjectID prefixRepresentsKey fields
ContentDocument069The file as a wholeTitle, FileType, FileExtension, ContentSize, OwnerId, LatestPublishedVersionId
ContentVersion068One version of the fileContentDocumentId, VersionData (the binary), PathOnClient, VersionNumber, IsLatest, Checksum
ContentDocumentLink06AOne place the file is sharedContentDocumentId, LinkedEntityId, ShareType, Visibility

The relationships, in plain text:

ContentDocument (069)
   |-- ContentVersion (068)  v1
   |-- ContentVersion (068)  v2   <-- IsLatest = true, holds current binary
   |
   |-- ContentDocumentLink (06A) --> Owner (User)
   |-- ContentDocumentLink (06A) --> Account 001...
   |-- ContentDocumentLink (06A) --> Opportunity 006...

What Happens When You Upload a File

  1. You insert a ContentVersion with the binary and a file name. This is the only one of the three you create directly.
  2. Salesforce creates the parent ContentDocument automatically and sets LatestPublishedVersionId to your version.
  3. Salesforce creates a ContentDocumentLink to the uploading user, and another to the record if the upload was made from a record page.

Upload a new version later and a second ContentVersion is added under the same ContentDocument. IsLatest moves to the new one. The links are untouched, so every record sharing the file now shows the new version.

ContentDocumentLink in Detail

This junction object is where most of the confusion lives.

  • LinkedEntityId is polymorphic. It can be a record of almost any object, a user, a Chatter group or a content library.
  • ShareType sets the permission: V viewer, C collaborator, I inferred from the user's access to the related record.
  • Visibility controls audience: AllUsers, InternalUsers or SharedUsers. This is what decides whether Experience Cloud users can see the file.
  • Query restriction. You cannot select from ContentDocumentLink without filtering on ContentDocumentId or LinkedEntityId, using an equals or IN condition. An unfiltered query returns an error. This single rule is why "list every file with its parent record" needs more than one query.

Queries You Will Use

All files on one record

SELECT ContentDocumentId, ContentDocument.Title,
       ContentDocument.FileExtension, ContentDocument.ContentSize
FROM ContentDocumentLink
WHERE LinkedEntityId = '001xx000003DGbYAAW'

All files on a set of records

SELECT LinkedEntityId, ContentDocumentId, ContentDocument.Title
FROM ContentDocumentLink
WHERE LinkedEntityId IN (SELECT Id FROM Opportunity WHERE IsWon = true)

The latest version of a file, with its checksum

SELECT Id, Title, PathOnClient, ContentSize, Checksum
FROM ContentVersion
WHERE ContentDocumentId = '069xx0000001AAA' AND IsLatest = true

Every record a file is shared with

SELECT LinkedEntityId, LinkedEntity.Type, ShareType, Visibility
FROM ContentDocumentLink
WHERE ContentDocumentId = '069xx0000001AAA'

File counts and sizes by type

SELECT FileType, COUNT(Id) files, SUM(ContentSize) bytes
FROM ContentDocument GROUP BY FileType ORDER BY SUM(ContentSize) DESC
Queries on ContentDocument and ContentVersion only return files the running user can access. For org-wide counts the user needs the Query All Files permission. Without it the numbers are quietly too low. See file export errors.

Where the Binary Lives

The file content is in ContentVersion.VersionData. A SOQL query does not return it as data. Through the REST API the field comes back as a URL, and the bytes are fetched with a separate request:

GET /services/data/v62.0/sobjects/ContentVersion/068xx0000002AAA/VersionData

One request, one file. There is no batch variant, which is why record export tools produce a CSV describing files and why a full file export costs one API call per file. We go into that in can the Bulk API export files?

How to Copy or Move a File Between Records

Because sharing is a link and not a copy, both operations are edits to ContentDocumentLink.

Copy (share the same file on a second record). Insert one row:

ContentDocumentIdLinkedEntityIdShareTypeVisibility
069xx0000001AAA006xx000004TARGETVAllUsers

No storage is consumed. The file now appears on both records, and a new version shows on both.

Move. Insert the new link as above, confirm it, then delete the original ContentDocumentLink row for the old record. Delete the link, never the ContentDocument, or the file is removed everywhere.

In bulk. Export the existing links for the source records with the "set of records" query above, replace LinkedEntityId with the target IDs in a spreadsheet, and insert the result with Data Loader. For a move, follow with a delete using the original link IDs.

An independent copy. If the two records need separate files that can diverge, download the binary and insert it as a new ContentVersion linked to the second record. This does use additional storage.

Moving files between orgs is a different job, since IDs do not carry over and the binaries have to be exported and re-uploaded. See moving files between orgs.

Notes, Attachments and Documents

TypeObjectPrefixLinked by
Salesforce FileContentDocument / ContentVersion069 / 068ContentDocumentLink (many records)
Enhanced NoteContentNote, stored as a ContentDocument with FileType = 'SNOTE'069ContentDocumentLink
Classic AttachmentAttachment00PParentId (one record)
Classic NoteNote002ParentId (one record)
DocumentDocument015Folder, not a record
Static ResourceStaticResource081None. Referenced by name.

Enhanced Notes appearing in ContentDocument counts surprises people, and so does the absence of Attachments from them. If you are sizing an export, count each row of this table separately. Attachments have their own guide.

What This Means for an Export

To export "all files on our opportunities" by hand you would query the links for the opportunities, collect the document IDs, look up the latest version of each, request each binary separately, decide what to do when one file is linked to four opportunities, and build a folder structure from the results. Each step is simple and the whole is a small program.

SFDC File Exporter does that resolution for you. You choose the object, list view, CSV of record IDs or SOQL filter. It follows the links, fetches the latest version of each file, and writes it under the parent record. It handles the other five storage types in the table as well, and requires nothing to be installed in the org.

Frequently Asked Questions

What is the difference between ContentDocument and ContentVersion?

ContentDocument represents the file as a whole: its title, owner and a pointer to the latest version. ContentVersion represents one specific version of that file and holds the actual binary content in its VersionData field. Uploading a new version of a file adds a ContentVersion and leaves the ContentDocument in place.

What is ContentDocumentLink in Salesforce?

ContentDocumentLink is the junction object that shares a file with a record, user, group or library. Each row connects one ContentDocument to one LinkedEntityId. A file attached to three records has three ContentDocumentLink rows plus one for its owner, and still only one ContentDocument.

How do I find all files attached to a Salesforce record?

Query ContentDocumentLink filtered by the record: SELECT ContentDocumentId, ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId = 'the record ID'. Salesforce requires a filter on ContentDocumentId or LinkedEntityId for this object, so you cannot select every link in the org in one unfiltered query.

How do I copy a Salesforce file to another record?

Create a new ContentDocumentLink with the same ContentDocumentId and the target record as LinkedEntityId. No data is duplicated and no extra storage is used: the same file now appears on both records. To move rather than copy, delete the original link afterwards.

Are Salesforce Notes stored as files?

Enhanced Notes are. A ContentNote is stored as a ContentDocument with a FileType of SNOTE, and it is linked to records through ContentDocumentLink exactly like a file. Classic Notes are different: they are records in the separate Note object with a ParentId.

Three Objects, One Rule

ContentDocument is the file, ContentVersion is its content, and ContentDocumentLink is where it appears. Edit links to copy or move. Delete links to unshare. Delete the document only when the file should be gone everywhere. Keep that straight and most Files questions answer themselves. When the question is "get them all out", SFDC File Exporter is the short route.

Skip the Queries and Export the Files

SFDC File Exporter resolves documents, versions and links for you and writes each file under its parent record. Free for 50 files per export.