Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rundeck executions cleanup
#!/bin/bash -e
# see related issue: https://github.com/rundeck/rundeck/issues/357
# export required vars
export RD_URL=http://localhost:4440 RD_USER=admin RD_PASSWORD=admin RD_HTTP_TIMEOUT=300
# make sure rd & jq commands are in the PATH
which -- rd jq >/dev/null
del_executions() {
local project=$1
while true; do
rd executions deletebulk -R -y -m ${RD_OPTION_BATCH:-20} --older ${RD_OPTION_OLDER_THAN:-3d} -p $project || break
sleep 1s
done
}
# delete executions for each project
for p in $(RD_FORMAT=json rd projects list | jq -r .[]); do
del_executions $p
done
exit 0
@DVGeoffrey

This comment has been minimized.

Copy link

@DVGeoffrey DVGeoffrey commented Nov 15, 2017

hello. why did you add the "while true" loop? what happens when you just run the deletebulk once?

@shamil

This comment has been minimized.

Copy link
Owner Author

@shamil shamil commented Dec 28, 2017

@DVGeoffrey I delete in batches, hence the loop. If I try to delete all jobs at once most probably I will get HTTP timeout from rundeck API.

@dandunckelman

This comment has been minimized.

Copy link

@dandunckelman dandunckelman commented Mar 1, 2018

@shamil, thank you for this.

@devinnasar

This comment has been minimized.

Copy link

@devinnasar devinnasar commented Aug 1, 2018

Hello,

The ''|| break" mechanic on line 14 isn't working for me. It seems that the script doesn't detect when there are no further executions to delete and it just loops forever on a given project. Any advice? I ended up doing this:

_delete_project_executions(){
  local _project="${1}"
  local _batch_size="${2}"
  local _max_age="${3}"

  printf "Deleting executions from project: \"${_project}\" older than: \"${_max_age}\" in batches of ${_batch_size}...\n"

  # Delete executions in $_project in batches of $_batch_size older than $_max_age.
  _executions_detected=true

  while [[ "${_executions_detected}" == true ]]; do

    exec {_stdout_copy}>&1 # make the FD named in "$stdout_copy" a copy of FD 1
    _executions_deleted=$(RD_FORMAT=json rd executions deletebulk --confirm --project "${_project}" --max "${_batch_size}" --older "${_max_age}" | tee /dev/fd/"${_stdout_copy}") # JSON format ensures either executions will be deleted or there will be no output.
    exec {_stdout_copy}>&- # close that copy previously created

    _executions_deleted_count=$(echo "${_executions_deleted}" | wc -l)
    printf "Executions deleted: ${_executions_deleted_count}...\n"

    sleep 1s

    if [[ -z "${_executions_deleted}" ]]; then
      _executions_detected=false
    fi

  done
}
@erik-stephens

This comment has been minimized.

Copy link

@erik-stephens erik-stephens commented Dec 7, 2018

@devinnasar, try -R:

[--require -R] : Treat 0 query results as failure, otherwise succeed if no executions were returned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment