Talk to Expert

Querying Salesforce Files with SOQL

Share this Article:

Salesforce file SOQL query
AI-Powered Reading

Explore This Article with AI

Get an instant summary, ask questions, or go deeper-open this page in your favourite AI tool in one click.

Learning how to use a Salesforce file SOQL query is essential for managing and auditing files efficiently in Salesforce. Whether you are auditing file storage, identifying orphaned records, or building custom file management tools, mastering how to query Salesforce files with SOQL gives you complete visibility and control over your org’s content.

Thank you for reading this post, don't forget to subscribe!

How Salesforce File SOQL Query Works

Before writing any SOQL query, it helps to understand how Salesforce stores file data. There are three core objects you will work with:

ContentDocument — the parent record that represents the file itself. It stores metadata like the file name, size, and owner.

ContentVersion — the child record that holds the actual file content and version history. Every time a file is uploaded or updated, a new ContentVersion is created.

ContentDocumentLink — the junction object that links a ContentDocument to a Salesforce record, such as an Account, Contact, or Opportunity.

Understanding these relationships is the foundation of any successful Salesforce file SOQL query.

Basic SOQL Query for Salesforce Files

To retrieve a list of all files in your org, start with a simple query on ContentDocument:

soql

SELECT Id, Title, FileType, ContentSize, CreatedDate, OwnerId

FROM ContentDocument

ORDER BY CreatedDate DESC

LIMIT 100

This returns file names, types, sizes, and creation dates — useful for a quick storage audit.

Querying ContentVersion for File Details

If you need the actual file content or version-specific metadata, query ContentVersion directly:

soql

SELECT Id, Title, FileType, ContentSize, VersionNumber,

      ContentDocumentId, CreatedDate

FROM ContentVersion

WHERE IsLatest = TRUE

ORDER BY CreatedDate DESCThe IsLatest = TRUE filter ensures you only retrieve the most recent version of each file, avoiding duplicate results across multiple versions.

Finding Files Linked to a Specific Record

To find all files attached to a particular Salesforce record — for example, an Account – query ContentDocumentLink:

soql

SELECT ContentDocumentId, ContentDocument.Title,

      ContentDocument.FileType, ContentDocument.ContentSize

FROM ContentDocumentLink

WHERE LinkedEntityId = ‘001XXXXXXXXXXXXXXX’Replace the LinkedEntityId value with the actual record ID. This is one of the most common Salesforce query file use cases for admins and developers.

Identifying Orphaned Files with SOQL

Orphaned files are ContentDocuments that are not linked to any record. These quietly consume storage without serving any purpose. Use this query to find them:

soql

SELECT Id, Title, ContentSize, CreatedDate

FROM ContentDocument

WHERE Id NOT IN (

   SELECT ContentDocumentId FROM ContentDocumentLink

)

Running this audit regularly helps you reclaim storage and keep your org clean – one of the best practices for Salesforce file management.

Filtering Files by Type or Size

You can narrow your queries further by filtering on FileType or ContentSize:

soql

SELECT Id, Title, FileType, ContentSize

FROM ContentVersion

WHERE FileType = ‘PDF’

AND ContentSize > 5000000

AND IsLatest = TRUE

This example returns all PDF files larger than 5 MB — ideal for identifying large files that may be contributing to storage limits.

SOQL Limits to Keep in Mind

When querying Salesforce files at scale, be aware of governor limits. SOQL queries in Apex are limited to 50,000 rows per transaction. For bulk file audits, consider using Batch Apex or the Salesforce Data Loader to handle large datasets safely without hitting these limits.

Final Thoughts

Learning to Salesforce file SOQL query with it gives admins and developers a powerful way to manage content, audit storage, and maintain a healthy org. Whether you are tracking down orphaned files, filtering by file type, or linking content to specific records, these queries form the backbone of any solid Salesforce file management strategy.

For large-scale file exports, pairing your SOQL knowledge with a dedicated tool like Files Downloader makes the process even faster – pulling thousands of files in their original formats without ever leaving your Salesforce environment.

[Book a Free Demo] | [View Pricing] | [Install on AppExchange]

Table of Contents

To query Salesforce files using SOQL, you need to work with three core objects — ContentDocument, ContentVersion, and ContentDocumentLink. A basic query on ContentDocument returns file names, types, sizes, and creation dates. For version-specific details, query ContentVersion with the filter IsLatest = TRUE to retrieve only the most recent file versions. These are the foundational SOQL queries every Salesforce admin and developer should know.

ContentDocument is the parent object that represents the file and stores metadata like file name, size, and owner. ContentVersion is the child object that holds the actual file content and tracks version history — a new ContentVersion is created every time a file is uploaded or updated. ContentDocumentLink is the junction object that links a ContentDocument to a specific Salesforce record such as an Account, Contact, or Opportunity. Understanding these three objects is essential for writing accurate Salesforce file SOQL queries.

Orphaned files in Salesforce are ContentDocuments that are not linked to any record via ContentDocumentLink. You can identify them using a SOQL subquery that returns ContentDocuments whose IDs do not appear in the ContentDocumentLink object. Running this Salesforce SOQL audit regularly helps reclaim storage, reduce bloat, and keep your org clean and well-maintained.

When querying Salesforce files at scale, SOQL queries in Apex are limited to 50,000 rows per transaction. If your org contains a large number of files, hitting this limit can cause query failures. To handle bulk file audits safely, it is recommended to use Batch Apex or the Salesforce Data Loader, both of which are designed to process large datasets without exceeding Salesforce governor limits.

You can filter Salesforce files by type or size by querying the ContentVersion object and applying conditions on the FileType and ContentSize fields. For example, filtering for FileType = 'PDF' and ContentSize > 5000000 returns all PDF files larger than 5 MB. Adding IsLatest = TRUE ensures only the most recent file versions are included. This approach is particularly useful for identifying large files that may be contributing to Salesforce org storage limits.

Setup → Quick Find → Salesforce Files → General Settings → Edit → Check "Skip triggers execution and validation rules on asset files" → Save