Copied to clipboard

Flag this post as spam?

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


  • Michael Nielsen 82 posts 362 karma points
    Apr 23, 2020 @ 15:20
    Michael Nielsen
    0

    Limit versions in Umbraco 8

    Hi

    Is there a way to limit the number of versions created when saving and publishing nodes in Umbraco 8?

    The Unversion package is v7 only.

  • Rodolphe Toots 35 posts 166 karma points
    May 11, 2020 @ 19:56
    Rodolphe Toots
    0

    Good question. Are there unlimited versions now? Sounds like database can grow unnecessary big if that is the case...

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    May 11, 2020 @ 20:48
    Nik
    0

    Hi Rodolphe,

    This was the case in v7 as well. There has always been the ability to had infinite versions saved. That is why someone created UnVersion to help remove historic versions, so it's not a new behaviour.

    Thanks

    Nik

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    May 11, 2020 @ 20:47
    Nik
    0

    Hi Michael,

    I believe you could look at the source of UnVersion and potentially do a very similar operation in your own custom event handler for save and publish in v8 to achieve the same thing.

    Thanks

    Nik

  • Michael Nielsen 82 posts 362 karma points
    May 20, 2020 @ 08:00
    Michael Nielsen
    100

    What I ended up with was doing this, which works fine.

    public class SaveComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            // Append our component to the collection of Components
            // It will be the last one to be run
            composition.Components().Append<SaveComponent>();
        }
    }
    
    public class SaveComponent : IComponent
    {
        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
            ContentService.Saving += ContentService_Saving;
        }
    
        // terminate: runs once when Umbraco stops
        public void Terminate()
        { }
    
        private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var content in e.SavedEntities)
            {
                // Delete versions older than 48 hours
                var date = DateTime.Now.AddDays(-2);
    
                sender.DeleteVersions(content.Id, date);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft