How can I clean my recycle bin automatically?
By creating a job scheduler in XWiki, you can effortlessly trigger recycle bin cleanups at any scheduled time, eliminating the need for manual navigation into Deleted Pages.
For deleted pages
To create a job scheduler in XWiki for cleaning the recycle bin from deleted documents, follow these step-by-step instructions:
Step 1: Navigate to the Job Scheduler Page
- Navigate from Navigation panel into XWiki / Job Scheduler.
- Or use the shortcut by typing "scheduler" in the search bar and selecting the first result suggestion.
Step 2: Add a New Job
- Scroll down the Job Scheduler page to the section where you can add a new job.
- Provide the name of the job as "Clean recycle bin from deleted documents" in the designated field.
- Click the "Add" button to create the new job.
Once the job is added, you'll be taken to the job details page.
Step 3: Fill in the Job Information
- In the "Job Name" field, make sure it says "Clean recycle bin from deleted documents" (it should already be filled in).
- Provide a brief job description like "This job will clean the XWiki recycle bin from deleted documents."
- Remove the existing Cron expression, if any, as we don't need it for this scenario.
- Add the Job Script, copy the following Groovy
select deldoc.id
from XWikiDeletedDocument as deldoc
where not exists (from XWikiDocument as doc where doc.fullName=deldoc.fullName)
"""
def deletedDocuments = xwiki.search(queryStatement)
if (!deletedDocuments.isEmpty()) {
deletedDocuments.removeAll()
}
Step 4: Even if we created a job, it's inactive by default with Job Status "None". It'll be set a bit later. Let's trigger our job.
- Return to the Job Scheduler page
- Found our newly created job
- Press trigger
That's all. We've cleaned our Recycle bin from deleted pages.
For deleted attachments
Repeat steps 1 to 3, but instead of using the previous Groovy script, use this one:
select attach.id from DeletedAttachment as attach
"""
def deletedAttachments = xwiki.search(queryStatement)
if (!deletedAttachments.isEmpty()) {
deletedAttachments.each{
void xwiki.getDeletedAttachment(it.toString()).delete()
}
}
Schedule jobs
As extra configuration, instead of triggering the job manually each time for our need, we can set Cron expressions for them. While editing our created jobs in CKEditor, you've already seen some usage examples. More can be found hereĀ Cron Trigger Tutorial.
Let's schedule our jobs so that they will be automatically executed on the last day of every month at 7 am. For this:
- edit each job in CKEditor
- and place thisĀ 0 0 7 L * ? in "Cron expression" field
- save changes
- finally, schedule this job by clicking on "Schedule"
Use the same expression for the deleted attachment removal operation. Note, that after this the status of the job will be changed to "Normal" and you'll see a date of next time of a run.