function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Unable to query all ContentDocuments

Hi - 
I have a probelm where I need to query back all content documents in my system to then run a scheduled job to clean up old documents. I am finding certian docuemtns I can only query if I explicitly set the Id in the WHERE clause, for example: 

FROM ContentDocument
WHERE Title LIKE 'Some Document'

This query returns no results. But if I do a search in Salesforce of "Some Document", document results come back with this title. I can grab one of those documents Ids and run a query like this: 

FROM ContentDocument
WHERE Id = '069...."

And the query would return a result with a title of "Some Document" 

I have tried adding the "USING SCOPE Team" qualifier in the "FROM" clause, and had some success with this, but I am still not able to query all documents in Salesforce. Does anyone have any suggestions on how I can query back ALL docuemnts? I have a system admin profile. 



Rejeesh Raghavan 2Rejeesh Raghavan 2

Hi Mikayla,

The below SOQL might work for you:-

SELECT Id FROM ContentDocument WHERE Id <> null LIMIT 100

This will fetch all the ContentDocument records for you, limited to 100 records.

It is advisable to fetch records using LIMIT keyword to avoid hitting governor limits, and process your scheduled job in batches.

Also, you would need a mechanism to mark the records as being processed for clean up to prevent the scheduler to reprocess the records.

Thank you for the response. 

Unfortuantely I am still not getting all records back with "WHERE Id <> null" 

Thank you for the additonal advice, the job is already set to run in batches and flags the records apporpirately, so I am not concerned there. It is frustrating when I am not able to query back all records, even though I can see records not in the query in the UI no problem.