Copied to clipboard

Flag this post as spam?

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


  • David Zweben 266 posts 750 karma points
    Jul 08, 2018 @ 14:56
    David Zweben
    0

    Simplest way to auto-delete nodes on date?

    I have a schedule section set up on my site, which uses 'schedule instance' nodes to store event dates for the schedule. I want these nodes to auto-delete once the event passes, as determined by the value of a date picker property on the node. What's the simplest way to do this?

    Preferably, I'd like the deletion to happen automatically, regardless of whether anyone is interacting with the website at the time. For the purposes of the project, it's important that it doesn't rely on any Umbraco packages, but .NET libraries are acceptable. Any suggestions are appreciated.

    Thanks,
    David

  • Yakov Lebski 554 posts 2118 karma points
    Jul 08, 2018 @ 19:06
    Yakov Lebski
    1

    You can schedule page to be unpublished at specific date and time. Umbraco supports it out of the box.

  • David Zweben 266 posts 750 karma points
    Jul 08, 2018 @ 19:09
    David Zweben
    0

    Hi Yakov,

    I know about the un-publishing capability, but I want the nodes to be deleted completely. Otherwise, the list of nodes is going to get too big over time, and someone would need to manually delete them.

    I know how I could cull the list of nodes on an event like page save, but I want it to happen automatically on a time interval instead.

    Thanks,
    David

  • Yakov Lebski 554 posts 2118 karma points
    Jul 08, 2018 @ 19:19
    Yakov Lebski
    1

    Another option is to subscribe to the unpublished event and delete node completely

    in application handler

     ContentService.UnPublished += ContentService_UnPublished;
    
     private void ContentService_UnPublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach (var item in e.PublishedEntities)
            {
                ApplicationContext.Current.Services.ContentService.Delete(item);
    
            }
        }
    
  • David Zweben 266 posts 750 karma points
    Jul 08, 2018 @ 19:21
    David Zweben
    0

    That's a pretty good idea, thanks.

  • Owain Williams 480 posts 1412 karma points MVP 6x c-trib
    Jul 08, 2018 @ 19:52
    Owain Williams
    0

    Hi Yakov, This is pretty nice - would it be possible to use this for only a specific document type? The reason I ask is, what if someone puts an unpublish date on say a News article and another on an Announcement. They want the Announcement deleted but not the News.

    O.

  • Yakov Lebski 554 posts 2118 karma points
    Jul 08, 2018 @ 19:56
    Yakov Lebski
    1

    I think it's should work in this way

         if(item.ContentType.Alias=="alias")
          {
    do logic here
     }
    
  • David Zweben 266 posts 750 karma points
    Jul 08, 2018 @ 20:01
    David Zweben
    100

    I also found this, which was helpful:

    https://growcreate.co.uk/blog/better-task-scheduling-in-umbraco/

    Using Umbraco's built-in scheduler looks like it will work well enough for me, so I'm going to make an Umbraco WebAPI controller to delete the nodes, and stick that URL in the scheduler config.

  • Yakov Lebski 554 posts 2118 karma points
    Jul 08, 2018 @ 20:16
    Yakov Lebski
    0

    Yes, you can do it this way too, when you run over all nodes and search node to delete, it may be very expensive for big sites.

Please Sign in or register to post replies

Write your reply to:

Draft