Is there any Sitecore API which can be used to clear the publish queue?
Occasionally (possibly related to Unicorn sync) I find that there are hundreds of publishing operations in the job viewer (/sitecore/admin/jobs.aspx). I would like to build an admin page which would allow me to clear this publish queue. Obviously, I could just write some crude SQL statements to delete the records in the PublishQueue
table, but it's better to use the API when possible.
-
/sitecore/admin/PublishQueueStats.aspx will allow you to cleanup a publish queue (at least that's what it says - didn't test it yet)– GatogordoJun 27, 2017 at 11:44
-
I think cleanup does not mean "remove all items in the publish queue"– Matthew DresserJun 27, 2017 at 11:54
-
because I tried that, and it did not make any change to the queued jobs.– Matthew DresserJun 27, 2017 at 11:56
-
Added the database provider method that the CleanupPublishQueue should lead to in the answer.. that might also help.– GatogordoJun 27, 2017 at 12:07
1 Answer
As said, the sitecore/admin/PublishQueueStats.aspx
will allow you to cleanup a publish queue so you might not have to write it yourself.
If you still want to, this is the code behind the cleanup button on that page:
protected void OnClick(object sender, EventArgs e)
{
Database database = Factory.GetDatabase(this.Databases.SelectedValue);
if (database == null)
{
...
}
else
{
TimeSpan timeSpan = DateUtil.ParseTimeSpan(this.IntervalToKeep.Text, TimeSpan.MaxValue);
if (timeSpan == TimeSpan.MaxValue)
{
...
}
else
{
PublishManager.CleanupPublishQueue(DateTime.Now - timeSpan, database);
}
}
}
So the API you are looking for is PublishManager.CleanupPublishQueue
.
This should call the CleanupPublishQueue
in the SqlProvider
:
public override bool CleanupPublishQueue(DateTime to, Sitecore.Data.DataProviders.CallContext context)
{
to = DateUtil.ToUniversalTime(to);
this.Api.Execute(" DELETE FROM {0}PublishQueue{1} WHERE {0}Date{1} <= {2}to{3}", (object) "to", (object) to);
return true;
}