Thursday, January 9, 2020

How to Delete all Files from Library using Apex

If you have to delete any library, then first you have to delete all the files present in that library. For this purpose you can run below mentioned simple apex script to delete all files from library.

string LibraryName='Demo Library';
ContentWorkspace ws = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE Name = :LibraryName LIMIT 1];
List<ContentDocument> contentDocumentIds = new List<ContentDocument>();
for(ContentDocumentLink con:[select id,LinkedEntityId,ContentDocumentId   from ContentDocumentLink  where LinkedEntityId=:ws.Id]){
    contentDocumentIds.add(new ContentDocument(id=con.ContentDocumentId));
}
system.debug('********contentDocumentIds:'+contentDocumentIds);
system.debug('********contentDocumentIds.size:'+contentDocumentIds.size());
database.delete(contentDocumentIds,false);


Hope this will help!!

No comments:

Post a Comment