+ Start a Discussion
liron169liron169 

Delete FeedItem + Attachment

Hi,
If I create FeedItem + Attachment and after a period I need to delete all the old FeedItem -for example, those that before 30 days.
<Note that all this is with apex code, not manually>

I understand that deleting the FeedItem is not deleting the attachment file, so I found that I can delete the attachments by querying ContentDocument object.

The problem is that I want to delete only the attachment that related to the feeds that were deleted, not all those in ContentDocument.
Assume that I can mark those attachments in some way (when creation) that will help to retrieve only them when need to delete them.

But perhaps I can avoid it? Does SF store in some way the connection between the FeedItem in its attachment?
Sonam_SFDCSonam_SFDC
Attachment(chatter file) information is stored on the feeditem(title, type, size etc) though if you see the file - it doesn't store any field which can relate it back to the feeditem it was attached with(http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_feeditem.htm)

Don't think there is a way to identify the same AFAIK.
liron169liron169
Hi Sonam,
I didn't clearly understand -
Does only the information of the file is being stored in the feedItem? Does it contain the URL to the place where the file actually stored?
Anuj_BakshiAnuj_Bakshi
Hi

I am having a similar issue.
I am able to delete the FeedItem(post) but am not able to delete the associated FeedAttachment.
Is there a resolution for this?

Thanks!
Naga  AlapatiNaga Alapati
Hi Liron,

I think you can do something like this using ContentDocumentLink
 
// Get all the feeds created less than 30 days
Date dt = Date.today().addDays(-30);
List<FeedItem> feedItems = [SELECT Id, ParentId, RelatedRecordId FROM FeedItem Where CreatedDate < :dt];

System.debug(feedItems);

// Get the parentIds of the feed items 
Set<Id> parentIds = new Set<Id>();
for (FeedItem fi : feedItems) {
	parentIds.add(fi.ParentId);
}

// Get the content document links. ContentDocumentLink is not supported for semi join inner selects
List<ContentDocumentLink> documentLinks = [SELECT Id, ContentDocumentId FROM ContentDocumentLink Where LinkedEntityId In :parentIds ];

Set<Id> documentIds = new Set<Id>();

for (ContentDocumentLink cdl : documentLinks) {
	documentIds.add(cdl.ContentDocumentId);
}

// Get the documents associated 
List<ContentDocument> documents = [SELECT Id FROM ContentDocument WHERE Id IN: documentIds];

System.debug(documents);

// if (feedItems.size() > 0) {
//	 delete feedItems;
// }
//
// if (documents.size() > 0 ){
//	 delete documents;
// }

The above code will not work as expected if the same content document is used in multiple feed items. I think you can use aggregrate query in those case scenarios.

To test the above code, I added an attachment to the opportunity feed and I'm able to get the documnet id associated to that feed

User-added imageUser-added image


Regards,
Naga
Naga  AlapatiNaga Alapati
Hi Anuj,

FeedAttachments will automatically deleted when you delete FeedItem. Can you check that once.

Thanks,
Naga
Anuj_BakshiAnuj_Bakshi

Hey Naga!

What I was doing:
I created a custom quick action button on the feed for the case object in which I was adding a contentversion and a contentdocumentlink to link it with the case. Then, I was adding a feeditem and feedattachment which I was associating with the contentversion I added early on.

When I used to manually delete the feeditem, the file was not getting deleted which was my problem.

What I was doing wrong:
As you said, deletion of feeditem will automatically delete the feedattachment.
When I delete the feeditem manually, the feedattachment was getting deleted but the conetntdocumentlink linking the contentversion to the feed was not so the file still showed in the related list. I simply stopped adding a contentdocumentlink and that solved my issue. So now the contentversion is linked from the feedattchment to the case and not through the contentdocumentlink.

However, there is still the issue with orphaned contentversion records taking space after the feeditem and feedattcahment is deleted but I am fine with that.

Thanks!