Tutorial: Schedule a Flow to Delete Old Files (ContentDocuments) – Featuring New ‘AddQuotesToFields’ Action, ExecuteSOQL, and ExtractStringsFromCollection v2
Check out the video walk-through here.
With all of the new actions coming I thought I’d share a ‘bring it all together’ tutorial that outlines several actions here on USF:
- ExecuteSOQL
- ExtractStringsFromCollection (Version 2!)
- (NEW!) AddQuotesToFields
- Sandbox Install Link
- Source Code (coming soon to the UnofficialSF repo)
I strongly recommend you reading up on Salesforce Files before tackling this, it will make the SOQL queries a lot easier to grasp.
One of the limitations (as many of you are aware) of the standard ‘Get’ elements of Flow is you that you cannot reference related fields like you can in SOQL. This usually isn’t a huge problem for standard and custom objects since you could in theory just create a formula field as a workaround. But what about objects you can’t create formula fields for like ContentDocumentLink?
Enter ExecuteSOQL. With this action you’ll be able to do the same things those fancy Apex guys and gals can do to manipulate and work with Files.
One “fun” limitation of querying ContentDocument is that you can’t use handy relationship queries for what you need – you have to get to them indirectly through ContentDocumentLink. The below example will not work:
SELECT Id
FROM ContentDocument
WHERE Id in (Select ContentDocumentID from ContentDocumentLink where LinkedEntityId in (blah blah blah)
What you’ll see developers frequently do is construct a List of ContentDocumentLinks and grab the ContentDocumentIds tied to each of them. Then they re-query ContentDocument to get their ContentDocument list which they can then update/delete/do whatever with. That’s what we’ll be doing in our second Flow!
View All Files Permission
Spring ’19 enabled the ‘View All Files’ permission which finally allows us to directly query all files in ContentDocument and ContentVersion. This is extremely handy – if you simply want to archive or delete ANY old file, you can now do that using simple SOQL queries or even a Get in Flow. You will need this enabled in Scenario 1.
Scenario 1 – Delete Old Files Posted by Community Users
Suppose you wanted to schedule a cleanup flow to delete old files posted by Community users – check out how you’d do this below!
NOTE: Always have fault paths on your actions! I skipped that for example-sake.






Scenario 2 – Delete Old Files Posted in Certain Accounts
A bit more advanced here – let’s say you wanted to clean up some files on a subset of records. In this case it would be a set of Accounts based on a Type field, but it could be any object. Check out the steps below on how you’d accomplish that.
NOTE: Always have fault paths on your actions! I skipped that for example-sake.







NOTE: If you don’t want to install this action you could still create a Loop to create your own ContentDocument collection.


