Deleting Files related to a record

Sample Code:

        Set contentDocumentIds = new Set();
        List cDocLink = [Select id, ContentDocumentId from
                                              ContentDocumentLink where LinkedEntityId = :ParentId];
        for(ContentDocumentLink contentDocLinks : cDocLink) {
        contentDocumentIds.add(contentDocLinks.ContentDocumentId);
        }
        delete cDocLink;
       
        delete [SELECT Id FROM ContentDocument
                                          WHERE Id in :contentDocumentIds];

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply