We currently have our dev team working out of VSTS, with git. The number of branches are getting quite large, and rather than manually deleting branches every month, I would like ability to auto-delete branches that have not been touched for, say, 60 days. I know this is possible to do in Jenkins, but we don't have Jenkins at the moment, and was wondering if this is possible to do in VSTS via some service hook?
-
When merging pull-request there is an option to automatically delete the branch, can it be a solution? – Andrii Litvinov Jul 05 '17 at 18:53
-
Let me ask this - I've seen teams leave branches around for a couple months before cleaning them up. What would be a reason for this? If none, then I don't see why that couldn't be a solution. – Greg Jul 05 '17 at 18:58
-
I don't see any reason to keep old branches. Especially with the intention to remove them in few month anyway. In all my projects we remove branches after merge. – Andrii Litvinov Jul 05 '17 at 19:02
2 Answers
VSTS is not support server-side hooks so far. But there have other ways to auto-delete the old branches in local machine. Detail steps as below:
1. In a certain directory (such as D:\script_for_git
), clone the VSTS git repo (only used for auto-delete branches).
2. Add a shell script (del.sh
) in the root git repo (D:\script_for_git\repo
) to delete the remote branches which were not changed 180 days (6 months) ago, the contents of the shell script as below:
git fetch origin
for reBranch in $(git branch -a)
do
{
if [[ $reBranch == remotes/origin* ]];
then
{
if [[ $reBranch ==remotes/origin/HEAD ]]; then
echo "HEAD is not a branch"
else
branch=$(echo $reBranch | cut -d'/' -f 3)
echo $branch
sha=$(git rev-parse origin/$branch)
dateo=$(git show -s --format=%ci $sha)
datef=$(echo $dateo | cut -d' ' -f 1)
Todate=$(date -d "$datef" +'%s')
current=$(date +'%s')
day=$(( ( $current - $Todate )/60/60/24 ))
echo $day
if [ "$day" -gt 180 ]; then
git push origin :$branch
echo "delete the old branch $branch"
fi
fi
}
fi
}
done
3. Schedule to run this shell script. There are many ways scheduling to run a script and it's OS related. Such as if you are using windows, you can refer this post. If you are using linux, you can refer this post.

- 36,876
- 5
- 61
- 74
I would suggest you to setup a scheduled build definition which will run git commands to figure out what needs to be deleted and delete those branches on server.
Useful Resources:

- 7,274
- 1
- 33
- 42