I have a trigger on ContentDocumentLink object. I have use case to show total number of Files and number of notes attached if the link was between Events and Content using Salesforce Content. And, offcourse, similar thing that needs to be done when a content is removed.


trigger ContentDocumentLinkTrg on ContentDocumentLink (after insert, before delete)  
    //Check if customization is not enable through custom settings
    String BeforeOrAfter = (Trigger.isBefore ? 'Before' : 'After');
    String TriggerType = ''
        + (Trigger.isDelete ? 'Delete' : '')
        + (Trigger.isInsert ? 'Insert' : '');
    system.debug(BeforeOrAfter + ' ' + TriggerType);
    // run TriggerHandler
    new ContentDocumentLinkTriggerHandler().run();


Checking the debug log, it is entering the AfterInsert event but not BeforeDelete. I see no restrictions in the SF documentation, can anyone put some light on this, please.


I'm having this issue on Lightning while on SF-Classic this functionality is working fine.

  • In this post is some info that may help: success.salesforce.com/answers?id=9063A000000ZlQjQAK, it seems to be a salesforce issue.
    – D. Cruz
    Commented Aug 4, 2016 at 14:35
  • Problem is- it's not official.
    – Mahmood
    Commented Aug 4, 2016 at 19:06
  • Yes, but this doesn´t mean the bug doesn´t exist. If you don´t find a solution to your problem, you can try the workaround suggested in this comments and try to create a trigger on the ContentDocument.
    – D. Cruz
    Commented Aug 4, 2016 at 19:09
  • The problem with the suggested approach is, in lightning experience, users can go to a file/note and then relate it to an event or any other entity. And likewise those can be unrelated. But the content (file/note) are still in the system only the link is created/broken. If I go with suggested solution, I have to rely on entire file/note deletion. Not gonna fulfill my requirement.
    – Mahmood
    Commented Aug 4, 2016 at 19:18
  • Please vote on this idea to have this fixed: https://success.salesforce.com/ideaView?id=0873A000000LqYeQAK
    – Wim
    Commented Jul 5, 2019 at 12:03

2 Answers 2


I know this is quite late response, I totally forgot about this question. Anyhow, just updating this question so that if anyone does encounter such an issue, he/she may find solace.

I'm not sure if this is a bug or by-design, but when you delete a Content link between two objects in Salesforce Lightning, ContentDocumentLink trigger works fine but when a user delete the content in Salesforce Classic from the Files or Notes related list following happens

  • file or note itself gets deleted but the ContentDocumentLink trigger doesn't fire
  • ContentDocumentLink is also not fired if user deletes the file/note through Chatter

I believed it should, as the File/Note is deleted its link also gets deleted but the trigger never fired. I logged a case with Salesforce but as usual for technical queries I didn't get any helpful response.

So I implemented two more triggers to handle this

  1. a trigger on ContentDocument object to handle deletion of Note/File itself.
  2. a trigger on FeedItem object to handle content deletion from Chatter.

I have been hitting a similar issue recently, after some discussion with a colleague and head scratching I worked around this limitation in a slightly different way. As this question was one of the first I came across when investigating, I thought it might be helpful to share the approach taken to resolve the issue when deleting a content document in lightning.

When deleting the content document in lightning, unless the user selects the "Remove from Record" option in the files list, the ContentDocumentLink after delete trigger does not run.

To work around this the following can be implemented on the ContentDocument trigger handler so that the ContentDocumentLink trigger handler is correctly called to do any clean up tasks.

public static void handleAfterDelete(Map<Id,ContentDocument> oldContentDocumentMap) {

public static void handleDocumentLinkDeleteSteps(Set<Id> contentDocumentIds){
    List<ContentDocumentLink> deletedContentDocumentLinks = [Select Id, LinkedEntityId, ContentDocumentId From ContentDocumentLink Where ContentDocumentId IN :contentDocumentIds ALL ROWS];

This then accounts for the lack of ContentDocumentLink after delete trigger event by calling the process for the deleted records. One thing to note, the fields on the ContentDocumentLink required in the trigger must be included in the query in the future method.

You must log in to answer this question.

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