Skip to content

AEM – Garbage Collection Automation

Screen Shot 2018-06-18 at 6.19.57 PM


Performing Garbage Collection on an AEM instance. Which is Automated using cURL and Jenkins. In this post we are focusing on AEM deployed on an Windows Server. So, powershell script will be used to trigger the Garbage Collection.


  • AEM – it is assumed that AEM instance is up and running
  • cURL – it is assumed that cURL is already installed and configured on the windows server
  • Jenkins – it is assumed that Jenkins server is up and running and also assumed that you already know how to create a Job on Jenkins


  • Script will be modified in upcoming updates to accommodate the respective AEM version.
  • As of Now script will run on AEM 6.3 ONLY


  1. We shall check if we are running Garbage Collection on Author/Publish and based on that we shall set the CQ_PORT to 4502/4503 appropriately
  2. we shall check the OAK Version of the AEM instance and based on the OAK version we shall determine which AEM version we are running
  3. Based on the AEM version we shall trigger the appropriate Garbage Collection command.


Step-01: Add the Project Description

1 Description
Description of the Jenkins Job

Step-02: Setup House Keeping to minimise the no# of builds

2 House Keeping
House keeping Jenkins by restricting the max # of build and retention

Step-03: Define Parameters for the Build – Remote Host Parameter

3 Parameter Computer
Remote Host on which the Garbage Collection will be Triggered
4 Parameter User
Username for logging-in to Remote Host
5 Parameter Password
Password for logging-in to Remote Host
6 Parameter AEM Instance
Type of AEM instance – Author/Publish
7 Parameter AEM User
Username of the AEM instance
8 Parameter AEM Password
Password of the AEM instance

Step-04: Build step PowerShell Script

# stopping the job if it encounters error
$ErrorActionPreference = 'Stop'

# Credentials are stored in env and dynamic variables
$SecurePassword = $env:Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $env:User, $SecurePassword

# Parameters that are dynamically injected during runtime

# Invoke a command on the remote machine.
Invoke-Command -ComputerName $env:Computer -Credential $cred -ScriptBlock {

# calling Dynamically injected Parameters

# Path of the executable cURL
$CURLEXE = 'C:\Program Files\curl76\bin\curl.exe'

# Checking AEM Instance Type and seting appropriate PORT
    switch ( $AEMENV )
        Publish     { "We are running Garbage Collection on $AEMENV at $HOSTNAME"
        Author      { "We are running Garbage Collection on $AEMENV at $HOSTNAME"
        AuthorMongo { "We are running Garbage Collection on $AEMENV at $HOSTNAME"

# Checking the AEM OAK Version to determine the AEM version
$url = "http://${HOSTNAME}:${CQ_PORT}/crx/explorer/index.jsp"
$webContent = Invoke-WebRequest -Uri $url -UseBasicParsing
$webContent.Content -match "(?.*)" | out-null
$OAK_VERSION =  $matches['title'] | %{ $_.Split(' ')[3]; }
$NEW_OAKVER = $OAK_VERSION | %{ $_.Split('.')[1]; }

# Setting the AEM version
    switch ( [int]$NEW_OAKVER )
        0 {"$HOSTNAME is running Oak $OAK_VERSION on AEM 6.0"}
        2 {"$HOSTNAME is running Oak $OAK_VERSION on AEM 6.1"}
        4 {"$HOSTNAME is running Oak $OAK_VERSION on AEM 6.2"}
        6 {"$HOSTNAME is running Oak $OAK_VERSION on AEM 6.3"}
        default {"Can not determine OAK Version. Aborting.."

# Needs some work to be done here to add other AEM versions, as of now works on AEM 6.3
# cURL command to trigger the AEM Garbage Collection
$GCURL= "http://${HOSTNAME}:${CQ_PORT}/system/console/jmx/org.apache.jackrabbit.oak:name=Segment+node+store+blob+garbage+collection,type=BlobGarbageCollection/op/startBlobGC/boolean,boolean"
$CurlArgument = '-s', '-o', '/dev/null',
                '--user', "${CQ_USER}:${CQ_PASS}",
                '-w', "%{http_code}",
                '-X', 'POST',
                '--data', "markOnly=true&forceBlobIdRetrieve=false"
$HTTP_RES_CODE = & $CURLEXE @CurlArgument

    switch ( $HTTPRESCODE_SW )
        20      {"BlobGC Successfully Triggered."}
        40      {"That didn't work. HTTP $HTTP_RES_CODE received."
        50      {"Server error. Result code $HTTP_RES_CODE received; fix the server then try again."
        default {"$HTTP_RES_CODE received; I have no idea how I got here. Aborting."
} -ArgumentList $HOSTNAME,$AEMENV,$CQ_USER,$CQ_PASS<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Step-05: Jenkins Build Options to trigger the Build

9 Build Options
Jenkins Build Options

Step-06: Console Output of Build Job

10 Console Output
Console Output – Job well done.

Shashi View All

A passionate devops and automation engineer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: