Copado Branches Cleanup

Updated 1 year ago by Copado Solutions

Copado's branching strategy creates feature and promotion Git branches to move changes across your pipeline and keep all org branches in sync. Over time, this can result in a considerable number of branches, especially if developers tend to validate and promote often. 

Even though the number of branches does not have a significant impact on the size of your repository, reducing the amount of branches will help improve the repository navigation as well as gain efficiency.

Now the question is: What branches can you safely delete from your repository?

You will probably have to reuse your feature branches in order to back promote your changes, and you may need to keep your successful promotion branches for future audits.

The best answer is all validation branches and unsuccessful promotion branches that are older than three months. 

How to Remove These Branches Using Copado?

How to Manually Delete Branches 

As explained in the article Manage Git Branches, you can browse through the repository record in Copado and select the Git branches you want to delete.

The downside of using this option is that it is not possible to determine whether the deployment associated with the promotion branch ended up successfully or not. However, you can use it to filter out your validation branches. Validation branches follow a naming convention that includes Validate in the name of the branch. 

To manually delete a branch, follow the steps below:

  1. Open a Git Repository record in Copado.
  2. Click on Manage Git Branches
  3. In the grid, type in Validate in the Name column. 
  4. Click on Select All to select all the filtered branches.
  5. Next, click on Delete Selected Branches
  6. In the modal, type in the word DELETE (using uppercase) and click on Delete to confirm.
How to Automatically Delete Branches

To automatically delete branches, follow these steps:

  1. Create a Deployment record where the destination org is the org where Copado is installed. The source org can be any org. You can name the deployment 'Copado Branches Cleanup'.
  2. Add an Apex deployment step and add the below code snippet. This will get the names of the branches to be deleted, create a JSON with the list of branches and send a request to trigger the Copado webhook. This will also delete the records of the validation deployments and the unsuccessful deployments since they are no longer required. 
This script is an example. You can modify it to add your own conditions or include a limit clause in the event of hitting Apex limits on the number of branches retrieved. It is recommended to previously test your script in the developer console in order to verify that the deployment won’t fail due to compilation errors. 
//This will set the timeframe of the branch creation. We want to delete validation and unsuccessful promotions older than three months. 
Date someDaysAgo =;

//This will prevent to remove any Org branch. Set the branches name according to the branches of your repository.
List<String> keepThoseBranches = new List<String>{'master', 'stage','test','preprod','dev','uat','int'};

String repositoryId = 'xxxx'; //Replace with the Salesforce Id of your repository.

String copadoAPIKey = 'xxxxx'; //Replace with your API Key.

Set<Id> oldDeployments = new Set<Id>(); //Set of old Deployments. Will be filled and used by the script.

//This will query the steps associated with branches. All Validation steps and unsuccessful git promotions.
String bodyJSON = '{"gitBranches":[';

String bodyEnd = ']}';

List<copado__Step__c> oldsteps = [SELECT
LastModifiedDate <= :someDaysAgo
AND copado__Branch__c != null
AND copado__Deployment__r.copado__Promotion__c != null
copado__Status__c ='Completed with Errors'
OR ( copado__CheckOnly__c = TRUE
AND copado__Status__c = 'Completed Successfully'
ORDER BY copado__Status__c

// LIMIT 10 You can set a limit here if required

//This will get the branch names and add to JSON String

Set<String> oldPromotionBranchNames = new Set<String>();

for( Integer i = 0; i< oldsteps.size(); i++){



bodyJSON += '"' + oldsteps[i].copado__Branch__c + '"';

if(i < oldsteps.size()-1){

bodyJson += ',';




bodyJSON += bodyEnd;


//This will set the Endpoint URL

String URL = '' + repositoryId +'?api_key=' + copadoAPIKey;

//This will launch the Webhook

// Instantiate a new http object

Http h = new Http();

// This will instantiate a new HTTP request, specify the method as well as the endpoint

HttpRequest req = new HttpRequest();



req.setHeader('Content-Type', 'application/json;charset=UTF-8');


// This will send the request, and return a response

HttpResponse res = h.send(req);

System.debug('request response: ' + res.getBody() );

//Get Deployment records for Deletion


delete [SELECT Id FROM copado__Deployment__c WHERE Id IN :oldDeployments];

If you are on the EU server, use this URL instead of the one provided in the script: '' + repositoryId +'?api_key=' + copadoAPIKey;

Also, make sure the URL in your script matches what you have in your Copado's remote site settings (Setup > Remote Site Settings > Copado).

  1. Save the deployment.
  2. Open the Scheduled Jobs tab and create a new record.
  3. Name the scheduled job 'Copado Branches Cleanup'.
  4. Click on Look up Copado Webhook and select Execute a Deployment.
  5. In the deployment lookup, select the deployment created in step 1. 
  6. Select your user to run the scheduled job and click on Save.
  7. Click on Schedule and select the desired frequency for the cleanup deployment to be executed, for instance, every Sunday.  
Please note that Copado is not responsible for any branches deleted by mistake.

Creating or editing scripts is out of the scope of Copado Support. If you would like our Professional Services team to help you with this configuration, please reach out to your customer success manager.

How did we do?