<p><em>"This is great, but I want to run a command/script when a branch is removed, how can I have jenkins run a command when a branch is removed?"</em></p>

<blockquote>
  <p>You will need to use a <strong>webhook</strong> for this, I use the following
  settings in Github:</p>
</blockquote>

<p><a href="https://i.sstatic.net/YKD6T.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YKD6T.png" alt="enter image description here"></a></p>

<blockquote>
  <p><strong>Bitbucket</strong> only supports the following webhooks:</p>
</blockquote>

<p><a href="https://i.sstatic.net/c1nAi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/c1nAi.png" alt="enter image description here"></a> </p>

<p><em>"Is there a way to have Jenkins trigger a job before it removes the branch?"</em></p>

<blockquote>
  <p>Not that I can see, but in Github you can create a custom label and
  trigger a job via a webhook when the label is added to a branch.</p>
</blockquote>

CC BY-SA 4.0
2021-09-17T01:37:46.300Z
Ok but how do you detect in Jenkins that you're building because of a branch deletion vs a push to the branch?
justin.m.chase
CC BY-SA 3.0
2017-12-21T07:36:36.757Z
2017-12-21T07:36:36.757Z
Kyle Steenkamp
<p>I  am using jenkins multibranch pipelines. If a branch is merged and deleted jenkins removes the branch from the pipeline as well.</p>

<p>This is great, but I want to run a command/script when a branch is removed, how can I have jenkins run a command when a branch is removed? Is there a way to have jenkins trigger a job before it removes the branch?</p>

<p>Edit: I should add I'm using bitbucket. It would be neat if Jenkins could trigger an event when purging a branch of a multibranch pipeline. Ideally it wouldn't matter what my git provider is.</p>

CC BY-SA 3.0
2017-11-17T19:17:55.240Z
For Bitbucket: https://dengelonsoftware.blogspot.fi/2017/01/jenkins-pull-request-and-pipeline.html?m=1
mico
CC BY-SA 3.0
2017-12-18T17:54:50.193Z
How about webhooks?https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html
mico
<jenkins><git><jenkins-plugins><jenkins-pipeline><jenkinsfile>
How do I trigger scripts on a merge or when a branch is deleted?
red888
# How do I trigger scripts on a merge or when a branch is deleted?

(Asked by: username_0 on 2017-11-16)

I am using jenkins multibranch pipelines. If a branch is merged and deleted jenkins removes the branch from the pipeline as well.

This is great, but I want to run a command/script when a branch is removed, how can I have jenkins run a command when a branch is removed? Is there a way to have jenkins trigger a job before it removes the branch?

Edit: I should add I'm using bitbucket. It would be neat if Jenkins could trigger an event when purging a branch of a multibranch pipeline. Ideally it wouldn't matter what my git provider is.

**Tags:** jenkins, git, jenkins-plugins, jenkins-pipeline, jenkinsfile

> **Comments:**
> **username_1**: For Bitbucket: https://dengelonsoftware.blogspot.fi/2017/01/jenkins-pull-request-and-pipeline.html?m=1  
> **username_1**: How about webhooks?https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html  

---

## 1 answers

### Answer by username_2 on 2017-12-21. Score: 4 | Accepted answer

*"This is great, but I want to run a command/script when a branch is removed, how can I have jenkins run a command when a branch is removed?"*

> You will need to use a **webhook** for this, I use the following
> settings in Github:

[![enter image description here](https://i.sstatic.net/YKD6T.png)](https://i.sstatic.net/YKD6T.png)

> **Bitbucket** only supports the following webhooks:

[![enter image description here](https://i.sstatic.net/c1nAi.png)](https://i.sstatic.net/c1nAi.png)

*"Is there a way to have Jenkins trigger a job before it removes the branch?"*

> Not that I can see, but in Github you can create a custom label and
> trigger a job via a webhook when the label is added to a branch.

> **Comments:**
> **username_3**: Ok but how do you detect in Jenkins that you're building because of a branch deletion vs a push to the branch?  

---

