[SalesForce] How to delete latest version from ContentDocument

Is there any way we can delete Latest Version from ContentDocument?

I have tried deleting ContentVersion, but DML Operation Delete is not allowed on it.

I have tried deleting ContentDocument, but it deletes ALL versions and the document itself.

Best Answer

Yes, all the version will be deleted, check the doc here, ContentDocument

'When you delete a document, all versions of that document are deleted, including ratings, comments, and tags.'

Yeah, there is no standard way to do this. But you can copy the latest version to a new file, and delete all the old version, like this,

ContentVersion oldCV = [Select Id, VersionData, PathOnClient, ContentUrl, Title, PublishStatus, Origin from ContentVersion Order By CreatedDate Desc Limit 1];

ContentVersion newCV = new ContentVersion();
newCV.Title = oldCV.Title;
newCV.PathOnClient = oldCV.PathOnClient;
newCV.ContentUrl = oldCV.ContentUrl;
newCV.VersionData = oldCV.VersionData;
newCV.Origin = 'H';
insert newCV;
Related Topic