Custom Scheduled Jobs

You can use this option to create your own scheduled jobs to run code at regular intervals. To create a custom scheduled job, follow these steps:

  1. Add a Note for your own reference.
  2. Select a User who the code will run as.
  3. Enter how often you want the job to run on Interval/Cron Expression.

    Select Show Examples for example time periods.


  4. Add Inline Script to tell your job what to do. You can enter the code on Script or choose existing code on File.
  5. Select Add to schedule your job or Run Now to execute the script now.

Example: Delete Old Comments Once a Month

If you find that you have a lot of old comments on Confluence pages, you can delete these at a regular interval using a custom scheduled job by following these steps:

  1. Enter Delete Old Comments Monthly for Note
  2. Select Admin for User.
  3. Enter 0 0 0 ? 1/1 MON#1 * for Interval/Cron Expression to run at 12:00 AM on the first Monday of the month.
  4. Enter the following code for Inline Script

    groovy
    def olderThan = "4w" def deletedComments = 0 def oldComments = get("/wiki/rest/api/search") .queryString("cql", "type=comment and lastmodified < now('-${olderThan}')") .asObject(Map).body.results oldComments.each { def contentId = it.content.id def response = delete("/wiki/rest/api/content/${contentId}").asString() if (response.status == 204) { logger.info("Successfully deleted comment with id: {}", contentId) deletedComments++ } else { logger.error("Error Status: {} - Failed to delete comment {} for page with id {}", response.status, it.title, contentId) } } logger.info("Successfully deleted {} comments ", deletedComments)
  5. Select Add
On this page