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.

  • May I know how did you deleted your ContentVersion/ContentDocument, It will be helpful if you provide me some doc/code. ( I want to delete my Chatter Attached file from one of my VF page) – Salesforce Steps Jun 10 '15 at 5:59

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;
  • so there is no way I can delete only the Latest version and restore previous version? – VarunC Jun 15 '13 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.