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
Goran K.Goran K. 

delete orphaned files

Hi,
I need to periodically delete orphaned files with a Scheduled Flow. Ie. files that don't have a ContentDocumentLink pointing to either Contact, Account, Case, Opportunity. 
I am having problem with formulating the query that can be used for this purpose.

Any ides anyone?

-Goran

Best Answer chosen by Goran K.
Sean Fielding 16Sean Fielding 16
Flow has a lot of limitations with files.  If you want to use flow, I would recommend a good portion of the logic exist in an invoccable method.  You might still find issues though as some of the query capabilities are also limited.

We've offered this as a service for a while.  We build a small app that we used to help perform the analaysis for dealing with the retroactive data...  https://sproketlogic.com/dealing-with-files-orphaned-files/

All Answers

VinayVinay (Salesforce Developers) 
Can you elloborate more on this?

Thanks,
Goran K.Goran K.

Yes

So due to the GDPR rules in Europe, we are required to delete all data about the users when they request it.
What usually happens is that contact with its account, case, interests and opportunity gets deleted manually by our helpdesk employees.
The files which have ContentDocumentLink pointing to these objects are not deleted automatically so we want to be able to locate those files and remove them from Salesforce org.
In other words, we want to be able to find the orphaned files if they no longer have a pointer to either contact, case, oppty, interest.
Our guess is that means to delete ContentDocument with its ContentVersion and ContentDocumentLink.

I noticed that when a parent object is deleted that ContentDocument PublishStatus switch from 'P' to 'R' and FirstPublishLocation.Type = 'User'. So if a files was related to a Contact first, after Contact gets deleted it gets related to the User. User is still Owner but the FirstPublishLocation changes.

This query will return ContentVersion with the two filters:
SELECT Id, Title, FileExtension, PublishStatus , Owner.Name, CreatedDate, FirstPublishLocationId, FirstPublishLocation.Type 
FROM ContentVersion 
WHERE PublishStatus = 'R' AND FirstPublishLocation.Type = 'User'

So I wish to find out if this is enough to filter out 'orphaned' files. And how to find related ContentDocuments and ContentDocumentLinks to delete those too.
I hope it can be done with a Flow, but we can also use Batch Apex.

Thanks.
-Goran

 

Michael Kolodner 15Michael Kolodner 15
@goran - Were you ever able to make progress on this?
Goran K.Goran K.
@michael well yes and no. I gave up using flow due to the file access limitations and started using scheduled batch jobs.
Sean Fielding 16Sean Fielding 16
Flow has a lot of limitations with files.  If you want to use flow, I would recommend a good portion of the logic exist in an invoccable method.  You might still find issues though as some of the query capabilities are also limited.

We've offered this as a service for a while.  We build a small app that we used to help perform the analaysis for dealing with the retroactive data...  https://sproketlogic.com/dealing-with-files-orphaned-files/
This was selected as the best answer