How can I clean my recycle bin automatically?

Last modified by Nikita Petrenko on 2025/01/17

Warning

Once you trigger the job to clean the recycle bin from deleted documents and attachments, there is no way to undo or restore the deleted items automatically. The job script provided is designed to permanently remove all deleted documents from the recycle bin, freeing up space and optimizing the XWiki environment. We recommend taking a backup of your XWiki instance before running the job to ensure you have a copy of the deleted pages in case you need them in the future. Only proceed with the job if you are certain about permanently deleting the content from the recycle bin.

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.

Information

Before proceeding, it is essential to have the following prerequisites:

  1. Be advanced Admin users
  2. Ability to see hidden pages
  3. Basic knowledge of Groovy syntax and XWiki database structure
  4. Understanding of Scheduler Application

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.

navigate into job page.png

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.

create new job.png

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
def queryStatement = """
    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()
}

fill job details.png

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

trigger the job.png

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:

def queryStatement = """
    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

cron expression.png

  • save changes
  • finally, schedule this job by clicking on "Schedule"

schedule the job.png

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.

final job.png

Get Connected