Obtaining all ContentDocument records using SOQL

Just found out today that with the Spring 19' update there is a new setting in the "Setup -> Permission Sets -> (choose an existing or create a new one) -> App Permission -> Content -> Query All Files: Allows View All Data users to SOQL query all files in the org".

This is not enabled by default for the admin user but you can at least enable it in a Profile/Permissions Set.

Also, now documented in the API Documentation.

Have fun!


As advised by David Ha, you should add a "USING SCOPE" option in your SOQL query in order to retrieve all the ContentDocument records. Strangely, I found that "USING SCOPE Everything" restricts the data to Owned files only, while "USING SCOPE Team" gives all ContentDocument regardless of Owner and sharing.

So this query did the trick for me (on API version > 32) :

Select Id, Title, FileExtension, CreatedDate From ContentDocument USING SCOPE Team

If you want to delete files that are related to a single object then export ContentDocumentIds from below query and then use those ContentDocumentIds to delete files which are related to object.

SELECT ContentDocumentId,ContentDocument.CreatedDate,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM User)

You can replace User with your ObjectApi name.