3

Does deleting a ContentDocument delete associated ContentDocumentLinks?

4

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

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.

  • +1 @jayant weird it works differently in classic and lightning – RedDevil May 9 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 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 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 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.