Following code with correct WHERE condition can be used to delete files attached to FeedItem after deleting FeedItem in Salesforce.
Sample Code:
Set < Id > setContentVerIds = new Set < Id >();
List < ContentDocument > listFiles = new List < ContentDocument >();
for (
FeedItem FI : [
SELECT Id, RelatedRecordId
FROM FeedItem
]
) {
//Add your required WHERE condition
if (
String.isNotBlank(
FI.RelatedRecordId
)
) {
//RelatedRecordId is the Content Version Id
setContentVerIds.add(
FI.RelatedRecordId
);
}
}
for (
ContentVersion CV : [
SELECT Id, ContentDocumentId
FROM ContentVersion
WHERE Id IN: setContentVerIds
]
) {
listFiles.add(
new ContentDocument(
Id = CV.ContentDocumentId
)
);
}
if ( listFiles.size() > 0 ) {
delete listFiles;
}