This doesn't work quite perfectly yet, with Jenkins 2.319.3, Folders Plugin 6.708.ve61636eb_65a_5, and Bitbucket Branch Source Plugin 751.vda_24678a_f781. I configured the "Orphaned Item Strategy" settings of a Multibranch Pipeline project as follows:
- Abort builds: yes
- Discard old items: yes
- Days to keep old items: empty
- Max # of old items to keep: empty
I opened a pull request, and Jenkins created a job for the pull request and added build #1 to the queue. Because the agents were busy, the build did not start yet. I then merged the pull request, and Jenkins immediately disabled the pull request job. However, build #1 was still in the queue. Finally, I clicked "Scan Multibranch Pipeline Now". Jenkins then removed Build #1 from the queue and deleted the pull request job.
It would be better to cancel the build immediately when the job is orphaned and disabled, not only when the job is deleted. I don't know whether it's possible to implement that just in cloudbees-folder-plugin or whether it would need changes in branch-api-plugin or workflow-multibranch-plugin as well.
The current implementation is in the DefaultOrphanedItemStrategy.orphanedItems method, which logs "Evaluating orphaned items". I got that log entry to "Scan Multibranch Pipeline Log" but not to "Multibranch Pipeline Events". Perhaps that means the orphanedItems method is not called during the webhook event.
Pull request opened at https://github.com/jenkinsci/cloudbees-folder-plugin/pull/197