30

Is there a way to tell Jenkins (2.2) to remove jobs for branches that were deleted? Currently my build-monitor fills up with branches as the jobs are not deleted. Is there a setting to trigger this behavior or should this be filed as a bug?

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
ligi
  • 39,001
  • 44
  • 144
  • 244

2 Answers2

24

Since you are using latest Jenkins version, consider switching your jobs to multibranch pipeline setup.

The Workflow Multibranch feature (provided by the workflow plugin) provides the following key abilities:

  • Automatic Workflow (job) creation in Jenkins per new branch in the repo (assuming webhooks are registered from GH to Jenkins).

  • Build specific to that child-branch and its unique scm change and build history.

  • Automatic job pruning/deletion for branches deleted from the repository, according to the settings.

  • Flexibility to individually configure branch properties, by overriding the parent properties, if required.

EDIT: check the 'Discard Old Items' settings for you pipeline. If you have non-zero 'Max # of old items' then, if your branch had less builds then this number, it will never be deleted! You can even see that in logs:

Will not remove US12345 as it is only #1 in the list
Will not remove US23456 as it is only #2 in the list
Will not remove US34567 as it is only #3 in the list

EDIT: You can also try your luck by adding additional behavior named "Prune stale remote-tracking branches" ("Behaviors" -> "Add" -> Select from dropdown).

madhead
  • 31,729
  • 16
  • 153
  • 201
  • So I will consider it a bug as I am using the multibranch pipeline setup - thanks – ligi May 13 '16 at 17:45
  • 3
    I'm seeing the same thing...the old branches aren't deleted from Jenkins when they're gone from the repository. @ligi: did you file a bug? – kenyee Feb 16 '17 at 20:33
  • 1
    Note that he current version of the Multibranch PIPELINE Plugin has a setting for deleted branches (and merged PR's) under "Orphaned Item Strategy" which offers a different policy than for the branches that are still active. I believe this would be the correct answer as of today. – Patrice M. Nov 21 '17 at 18:11
  • 3
    I am fighting this also, I though it was working, on the UI the pruned branches are no longer there, but on the disk the jobs are still there, the disk usage keep climbing. I can't get jenkins to automatically delete the items from disk – pogorman Feb 12 '18 at 19:20
  • This is still a major issue. – sridesmet Oct 23 '18 at 14:55
  • Any news on this as of today? – mgouin Nov 25 '18 at 04:48
  • I had this problem and it was due to no Scan Multibranch Pipeline Triggers being set, as described in https://issues.jenkins.io/browse/JENKINS-66970 – RjOllos Oct 26 '22 at 06:04
6

1) Go to Manage Jenkins -> Global Tool Configuration -> Git -> Add Git -> Choose JGit.

2) Then go to your multibranch pipeline configuration and choose JGit instead Default under Branch Sources -> Git Executable

3) Press Branch Indexing -> Run now

Old branches should go away now

StasKolodyuk
  • 4,256
  • 2
  • 32
  • 41