I have inserted a file in Library. I want to delete the ContentDocumentLink by which it is shared with the Library. I have created new ContentDocumentLink where I have shared file with a user. But while deleting the ContentDocumentLink of library files it is giving error :

Delete failed. First exception on row 0 with id 06A5B000002Q3GKUA0; first error: DEPENDENCY_EXISTS, Cannot complete this operation. You cannot remove a document from its owning library. : : []

Below is my code:

List<ContentDocumentLink> documentLinks = [select id,ContentDocumentId,LinkedEntityId,ShareType,Visibility from ContentDocumentLink where ContentDocumentId in : conDocumentIds];
    system.debug('documentLinks '+documentLinks);
    for(ContentDocumentLink c:documentLinks){
        ContentDocumentLink newCDL= new ContentDocumentLink();
        newCDL.ContentDocumentId = c.ContentDocumentId;
        newCDL.LinkedEntityId = vuserId;
        newCDL.ShareType = 'v';
        newCDL.Visibility = 'AllUsers';
        insert newCDL;
    delete [select id,ContentDocumentId,LinkedEntityId,ShareType,Visibility from ContentDocumentLink where LinkedEntityId=:libraryId];

Please help me to resolve this. Thanks in advance.


1 Answer 1


It has been a while but I expect if you delete the ContentDocument then the ContentDocumentLink will magically be deleted too.

I got the same error when trying to delete a ContentWorkspaceDoc. It works a little different from what you, but in the Files tab you basically have a ContentWorkspaceDoc instead of a ContentDocumentLink. The ContentWorkspaceDoc is a link between a ContentDocument and a ContentWorkspace (which is a Files Library i.e. the file folder).

I tested and counted the number of records of each type before and after deletion. I found that Salesforce does not allow you to delete the ContentWorkspaceDoc record. You can delete the ContentDocument, and when you do then Salesforce automatically deletes the linked ContentWorkspaceDoc and ContentDocumentVersion as well.

You must log in to answer this question.

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