6

Does deleting a ContentDocument delete associated ContentDocumentLinks?

2 Answers 2

6

Yes, deleting a ContentDocument will also delete any related ContentDocumentLink entities. I tested this out in my org:

ContentDocument d = [SELECT Id FROM ContentDocument LIMIT 1];
ContentDocumentLink[] l = [SELECT Id FROM ContentDocumentLink WHERE ContentDocumentId = :d.Id];
System.debug(l.size());
System.assert(l.size() > 0);
delete d;
l = [SELECT Id FROM ContentDocumentLink WHERE ContentDocumentId = :d.Id];
System.debug(l.size());
System.assert(l.size() == 0);
System.assert(false, 'Yes, they were deleted.');

The final assertion was set so that the transaction would roll back (i.e. the document would not actually be deleted). This script proves that deleting a ContentDocument causes a cascade deletion of any links.

Note that in the Classic UI, only the link is deleted, but the ContentDocument itself survives. When you delete the ContentDocument directly (e.g. via the API or Apex), regardless of platform, it will remove any associated links.

2
  • Does it also delete all ContentVersions?
    – fred
    Jul 21 at 17:20
  • 1
    @fred yes, ContentVersions will be cascade deleted.
    – sfdcfox
    Jul 21 at 17:58
4

I haven't tried it out myself, but looking at ContentDocument and ContentDocumentLink trigger behavior in Classic and Lightning knowledge article, it seems the behavior is different depending on the Experience - Classic vs. Lightning.

  • In Classic, ContentDocument triggers do not fire, as Salesforce only deletes the associated ContentDocumentLink record, not the ContentDocument record.

  • In Lightning Experience, both the ContentDocument and related ContentDocumentLink records are deleted, and by design Salesforce only fires the trigger on ContentDocument, not the trigger on ContentDocumentLink.

There's a detailed explanation and example on the link.

4
  • +1 @jayant weird it works differently in classic and lightning
    – RedDevil
    May 9, 2019 at 20:55
  • @RedDevil I have seen such kind of weird behaviors in triggers depending on the experience (classic vs. lex). One of such issues we had reported was this one.
    – Jayant Das
    May 9, 2019 at 21:03
  • This article refers to deleting the document through the UI. It does not describe what this question is asking.
    – sfdcfox
    May 9, 2019 at 22:30
  • @sfdcfox Could be possibly but the question in that regards is not complete as what is the context, if it’s from UI then this suffices. But if it’s at the platform level and not related to UI, then your answer provides that insight.
    – Jayant Das
    May 9, 2019 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .