Auto Delete Old GitHub Branches Using Jenkins

GitHub is an extremely useful tool if you have multiple developers constantly needing to make changes to the code base. However, the repository can quickly become cluttered with branches especially if you have every developer creating a new branch for each new feature and bug fix. Constantly having to manually delete old branches can be a pain. Luckily, Jenkins can be configured to have a job that automatically deletes GitHub branches older than a given date.



The job can be configured to connect to your GitHub repository using the Jenkins Git plugin. Within the job, under the Source Code Management section, specify which repository you wish to pull from and set branches to build as all remotes. From there you can enter the git commands you wish to run in the Execute Shell build option.



#!c:\cygwin64\bin\bash --login</code>

# Set cutoff date
cutoff_date=$(date --date="6 month ago" +%s)

# Retrieve commit dates and ref name
git for-each-ref refs/remotes --merged origin/stable --sort='committerdate' --format='%(committerdate:raw)/%(refname)' |

egrep -v "master|geneticAlgorithm|159637_GoCampaign|UnitTesting|47492-cobrowsing|152595-sitemap-fix|151409-import-contract-prices|142037-new-email|myColletteSprint1|mobile-responsive-images|offers-project-2|githost|161377|tourPricesOptimization|ModsToBooking|162388-marriott-remove-hot-deal" | while read branch; do

# Check if branch is older than cut off date
if ((${branch%/refs/remotes/origin/**} < $cutoff_date)) ; then

# Check for remote branch
CHECK_REMOTE_EXISTS='git branch -r | grep -e "^..origin/${branch#*/refs/remotes/origin/}\$"'

if [ -n "$CHECK_REMOTE_EXISTS" ]; then
echo "** DELETING remote branch ${branch#*/refs/remotes/origin/}"

# Delete the remote branch
git branch -d -r origin/${branch#*/refs/remotes/origin/}

# Push to remove it from origin
git push origin :${branch#*/refs/remotes/origin/}

echo "** Remote branch {$branch#*/refs/remotes/origin/} not found"





First step in deleting the branches is to return a list of all the remote branches commit dates and names. The command git for-each-ref allows you to do just that. You can set a format pattern using the “–format” option as well as sort that branches by commit date using “–sort”. You can also choose to only return branches that have been merged into another branch using the “–merged” option. In the example above, only branches that have been merged into the “stable” branch are returned.

After calling the git for-each-ref command, you can filter out branches that you never want to be deleted by calling “egrep -v” and then entering a string of the branch names. Once you have the correct list of branches with the appropriate format, you need to iterate over the branches and delete the branches that have a commit date older than the specified cutoff date.

Windows Integration

If you are using Jenkins on a Windows machine, the commands entered in Execute Shell will not be recognized. There are a couple of ways you can choose to run bash commands on a Windows machine. In the example above, Cygwin is used to run the bash executable. Simply, download Cygwin and run the setup executable. Make sure to select the required Git packages listed below so that the git commands will be recognized.


Finally, call the C:\cygwin64\bin\bash –login at the top of your Execute Shell command and you should now be able to run your delete branches script.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s