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.