Copied to clipboard

Flag this post as spam?

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


  • Leland 3 posts 94 karma points
    Feb 14, 2017 @ 21:06
    Leland
    0

    Clearing Recycle Bin on Schedule

    Hello,

    We are looking to implement a solution to clear our Umbraco recycle bin every week. I was hoping there would be an API call I can do from bash on a scheduled task to complete this but I am having a hard time finding information on that. What's the best way to go about doing this?

    Thank you!

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Feb 16, 2017 @ 08:28
    Dennis Adolfi
    103

    Hi Leland.

    So there is a Service call you can use that will clear the RecycleBin in the ApplicationContext.Current.Services.ContentService called EmptyRecycleBin().

    What I would recommend is to create a UmbracoApiController method that clears the recylce bin. Here is a little POC on how your ApiController could look. The url for this Api method will be http://yourdomain.com/Umbraco/Api/RecycleBinCleanup/ClearRecycleBin.

    using System;
    using System.Web.Http;
    using Umbraco.Core;
    using Umbraco.Core.Logging;
    using Umbraco.Web.WebApi;
    
    namespace YourApplication.App_Code
    {
        public class RecycleBinCleanupController : UmbracoApiController
        {
            [HttpGet]
            public string ClearRecycleBin()
            {
                try
                {
                    ApplicationContext.Current.Services.ContentService.EmptyRecycleBin();
                    LogHelper.Info<string>("RecycleBin Cleared at " + DateTime.Now);
                    return "OK";
                }
                catch (Exception exception)
                {
                    LogHelper.Error<string>("Could not emptyRecycleBin", exception);
                    return "Error";
                }
            }
        }
    }
    

    And then create a Windows Scheduled task on your server that runs on an exact time for example every sunday. https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx#BKMK_winui

    Or you could use Umbraco´s built in Scheduled Task, but those are based on intervals not exact time, but maybe that could work for you if you tweak the api method so that it checks the current dayOfWeek, or set it to a higher interval. Scheduled tasks are configured in the /config/umbracoSettings.config file in the scheduledTasks section. http://stackoverflow.com/questions/15415175/scheduling-to-run-a-macro-on-every-hour

    <scheduledTasks>
        <!-- add tasks that should be called with an interval (seconds) -->
        <task log="true" alias="clearRecycleBin" interval="60" url="http://yourdomain.com/Umbraco/Api/RecycleBinCleanup/ClearRecycleBin"/>
      </scheduledTasks>
    

    NOTE: The above task would actually clear your recycle bin every 60 seconds, and you probobly want a higher interval set, this was only set for testing.

    I hope this was helpful. Let me know how it goes.

    Take care!

  • Leland 3 posts 94 karma points
    Feb 16, 2017 @ 22:48
    Leland
    1

    Thank you Dennis! We are going to try and implement this and I will reply back with the status.

    Best, Leland

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Feb 17, 2017 @ 07:49
    Bjarne Fyrstenborg
    1

    If you want to run the task at a specific time, you can also trigger the method from Windows Task Scheduler, Azure Task Scheduler or another service to run the task at a specific time - or maybe try this package: Url Task Scheduler For V7

    To prevent (theoretically) any people to trigger the method, you can add a key as a parameter, e.g. a guid and match it with the value in a private variable or a key from web.config, although it doesn't totally prevent others from executing it, but most likely people won't guess the url.

    Maybe also only allow it to be executed within a specific time of the day.

    Alternatively you can use UmbracoAuthorizedApiController, but I think it then only is possible to request the method from backoffice, but it could be used in a dashboard for editors to list number of nodes in content and media recycle bin similar to TheDashboard package and then if necessary add buttons to clear these. Often I have seen the recycle bin containing a lot of nodes because editors sometimes never empty the recycle bin.

    /Bjarne

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Feb 17, 2017 @ 07:02
    Dennis Adolfi
    0

    Great. I would love to hear how it goes!

    Take care!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 17, 2017 @ 07:43
    Dave Woestenborghs
    2

    Hi guys,

    There is a possible security risk with this. If a malicious user finds out the url of your api than he can call this and clear the recycle bin all the time.

    In this case that would not be a huge risk. But if you have api calls that do other things this can mess up your site.

    Dave

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Feb 17, 2017 @ 07:56
    Dennis Adolfi
    1

    Good point Dave, thank you!

    He should probobly go for any of the tips Bjarne provided higher up in this thread.

    One way of solving it with the [Umbraco.Web.WebApi.UmbracoAuthorize] attribute (or a UmbracoAuthorizedApiController) would be to store a "Latest Cleared Date" and then create a scheduledTasks that would check this date and if its older than a week, clear the recycle bin and update the "Latest Cleared Date" with todays date.

    More on Api Authorization: https://our.umbraco.org/documentation/reference/routing/webapi/authorization

Please Sign in or register to post replies

Write your reply to:

Draft