API: Merge request creation ignores project's remove_source_branch_after_merge setting
Summary
When creating a merge request via the REST API (POST /projects/:id/merge_requests) without explicitly setting the remove_source_branch parameter, the API does not respect the project's configured remove_source_branch_after_merge setting. Instead, it defaults to false (unchecked), creating inconsistent behavior between UI-created and API-created merge requests.
Steps to reproduce
-
Configure a project with default "Delete source branch" enabled:
- Navigate to Settings → General → Merge requests
- Enable "Enable 'Delete source branch' option by default"
- Save changes
- Verify the setting via API:
GET /api/v4/projects/:idshows"remove_source_branch_after_merge": true
-
Create a merge request via the REST API without specifying
remove_source_branchparameter: curl --request POST
--header "PRIVATE-TOKEN: <your_token>"
--header "Content-Type: application/json"
--data '{ "source_branch": "test-branch", "target_branch": "main", "title": "Test MR via API" }'
"https://gitlab.com/api/v4/projects/:id/merge_requests" 3. Check the response and observe: { "force_remove_source_branch": false, "should_remove_source_branch": false } 4. Compare: Create an MR via the GitLab UI in the same project - the "Delete source branch" checkbox is checked by default (respects project setting)
Example Project
This bug can be reproduced on any GitLab.com project where:
- The project has
remove_source_branch_after_mergeenabled in settings - MRs are created via the REST API without the
remove_source_branchparameter
What is the current bug behavior?
When the remove_source_branch parameter is omitted from the API request:
- The merge request is created with
force_remove_source_branch: false - The "Delete source branch" checkbox is unchecked in the UI
- The project's
remove_source_branch_after_mergesetting is completely ignored - API-created MRs behave differently from UI-created MRs in the same project
What is the expected correct behavior?
When the remove_source_branch parameter is not provided in the API request:
- The API should check the project's
remove_source_branch_after_mergesetting - Apply that project default to the newly created merge request
- Behave consistently with how the GitLab UI creates merge requests
- Only use an explicit value when the
remove_source_branchparameter is actually provided in the request
This would make API-created MRs respect project-level configuration, just like UI-created MRs do.
Relevant logs and/or screenshots
API Response showing the issue: { "id": 123456, "iid": 83, "title": "Test MR via API", "source_branch": "test-branch", "target_branch": "main", "force_remove_source_branch": false, "should_remove_source_branch": false, ... }Project setting (confirmed enabled): { "id": 12345, "name": "example-project", "remove_source_branch_after_merge": true, ... }Current workaround (requires extra API call):
Must explicitly fetch project setting and pass it
PROJECT_SETTING=$(curl --header "PRIVATE-TOKEN: "
"https://gitlab.com/api/v4/projects/:id" |
jq '.remove_source_branch_after_merge')
curl --request POST
--header "PRIVATE-TOKEN: "
--data "{
"source_branch": "test-branch",
"target_branch": "main",
"title": "Test MR",
"remove_source_branch": $PROJECT_SETTING
}"
"https://gitlab.com/api/v4/projects/:id/merge_requests"### Output of checks
Results of GitLab environment info
Expand for output related to GitLab environment info
Reproduced on GitLab.com (SaaS) GitLab version: Latest (as of 2026-01-23) API version: v4
Results of GitLab application Check
Expand for output related to the GitLab application check
N/A - This issue is on GitLab.com
Possible fixes
The issue likely exists in the merge request creation service. When remove_source_branch is not provided in the API params, the service should:
- Fetch the project's
remove_source_branch_after_mergeattribute - Use it as the default value for
force_remove_source_branch - Only override when explicitly provided in the request
Potential location (approximate):
-
lib/api/merge_requests.rb- API endpoint handler -
app/services/merge_requests/create_service.rb- Service that creates MRs
The fix should ensure that omitting the parameter results in project default behavior, not hardcoded false.
Additional Context
Impact:
- CI/CD automation tools don't respect team preferences
- CLI tools (
glab, custom scripts, MCP servers) create MRs with wrong defaults - Inconsistent UX between UI and API
- Teams with "delete by default" enabled still accumulate stale branches from API-created MRs
Related:
- Issue #18283 - Original feature for configurable defaults
-
MR !29787 - Implementation of
remove_source_branch_after_merge - The project setting was added in GitLab 12.5, but the API may not have been updated to respect it
Patch release information for backports
Not applicable at this stage.