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!!

5 comments:

  1. They can't depend on traditional file transfer strategies like email, messenger administration and FTP when the files are an excessive number of and excessively enormous and should be sent at the earliest opportunity.transfer files

    ReplyDelete
  2. Twitter Promotion is a great way to have this blog seen but if you have videos you can also buy tiktok views and more !

    ReplyDelete
  3. You can try File ZIPO, a native appexchange application that can help you to Zip multiple Salesforce files & attachments from the Salesforce org and download them in: Single folder or Multiple folder (Parent / Child Structure)

    here's link - Zip Salesforce Files

    ReplyDelete