Automatically delete pages that have been archived for x days

Brandon White asked on October 4, 2018 20:44

I want to be able to automatically (or some easy manual way) delete pages that are archived after x amount of days (365 most likely). I set up an advanced workflow with an auto-transition from the Archived step to the Delete step after x days. In the scope of the workflow I set it to cover the root path with all child pages with a macro condition that the page is Archived: (CurrentDocument.DocumentIsArchived). When I look at the pages tab it doesn't add any pages to the scope. I'm guessing it is because all pages are also under the Standard workflow or any custom workflows we have applied. Is there a way to make this work in this way? Should I modify all the other workflows to include an additional Delete step or is there a better way to achieve this? Maybe a custom scheduled task? If there is a manual way where I can find archived pages based on date archived and mass delete that might be ok too, but an automatic process would be best. I also thought of creating an archive folder and applying the custom workflow to it, but then everyone would have to move the archived pages under it. Anyone have any ideas?

Recent Answers


Zach Perry answered on October 4, 2018 21:05

If you want to do it thru workflow, I think you would have to modify all your other workflows, and that to me feels like something would be missed, or not done right in the future.

I would probably do a scheduled task. I think you can get the documents doing something like this: DocumentHelper.GetDocuments().Where("(DocumentIsArchived = 1) AND (DocumentLastPublished < getdate()-30)");

Not positive is last published date is the correct field for the date, would have to do some testing to verify.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on October 4, 2018 21:52

It should be "DocumentModifiedWhen" as it will track the time the page is archived. DocumentHelper.GetDocuments().Where("(DocumentIsArchived = 1) AND (DocumentModifiedWhen < getdate()-30)");

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on October 8, 2018 20:57

I ended up with this which seems to work well:

using CMS;
using CMS.DocumentEngine;
using CMS.EventLog;
using CMS.Scheduler;

[assembly: RegisterCustomClass("Custom.ArchivedPageDeletion", typeof(ArchivedPageDeletion))]
public class ArchivedPageDeletion: ITask
{
    public string Execute(TaskInfo ti)
    {
        //// Gets the pages that will be deleted
        MultiDocumentQuery pages = DocumentHelper.GetDocuments()
                                    .Path("/Archive", PathTypeEnum.Children)
                                    .Where("(DocumentIsArchived = 1) AND (DocumentModifiedWhen < getdate() - 30)");

        // Loops through the pages
        foreach (var page in pages)
        {
            page.Delete();
            EventLogProvider.LogInformation("ArchivedPageDeletion", "Cleanup", string.Format("Page {0} was destroyed", page));
        }
        return "Finished";
    }

}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.