Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 04, 2020 @ 15:30
    Dan Diplo
    0

    Using Content Service to delete specific versions

    The Umbraco ContentService has a method to get all previous versions of a specified piece of content eg.

    var versions = Services.ContentService.GetVersions(1234);
    

    would return all the previous versions for a piece of content as IEnumerable<IContent>

    So far, so good.

    There is also a ContentService method called DeleteVersion(int Id, int versionId) which you pass the ID of the content and the versionID to delete. But where do you get his version ID from?

    The IEnumerable<IContent> returned by GetVersions() all have the exact same ID. Example:

    var versions = Services.ContentService.GetVersions(1234);
    
    foreach (var v in versions)
     {
                <p>@v.Name - @v.Id | @v.PublishedVersionId</p>
    }
    

    For an example page it would return:

    Example Article - 1149 | 130
    
    Example Article - 1149 | 130
    
    Example Article - 1149 | 130
    
    Example Article - 1149 | 130
    

    Each one is the same and has the same ID, published version ID etc. same create date etc. So how do you distinguish between them to target a specific version? My ultimate goal is to delete all versions except the most recent.

  • Kevin Jump 2309 posts 14673 karma points MVP 7x c-trib
    Mar 04, 2020 @ 16:12
    Kevin Jump
    100

    Hi

    I think PublishedVersionId is always the version that is published, while v.VersionId should return the specific version of the item you are looking at ?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 04, 2020 @ 16:28
    Dan Diplo
    0

    Thanks, Kevin, that seems to be correct. Rasmus on Twitter also tweeted that, too.

    So, based on that info and a few experiments I've determined that you can do something like this to delete all previous versions of a node except one, which was my original intent:

    int pageId = 1234;  // the Id of the node in question
    
    var version = cs.GetById(pageId);
    
    cs.DeleteVersion(pageId, version.PublishedVersionId, true);
    

    Assuming cs is a reference to the current ContentService.

    For anyone who might also stumble on this and care you can effectively do this for all content in your site like this:

    var cs = Services.ContentService;
    
    var siteContent = new List<IContent>();
    
    var rootContent = cs.GetRootContent();
    
    foreach (var content in rootContent)
    {
        siteContent.AddRange(cs.GetPagedDescendants(content.Id, 0, int.MaxValue, out long total));
    }
    
    foreach (var content in siteContent)
    {
        cs.DeleteVersion(content.Id, content.PublishedVersionId, true);
    }
    

    So this fetches all site content and then loops through it, deleting all the old versions. I'd always take a backup of your DB before doing this, of course, and take no responsibility for any mistakes that might arise from doing this :)

Please Sign in or register to post replies

Write your reply to:

Draft