1. Overview

Azure DevOps Repository is a version control tool used to manage and track file changes. When using it, issues may arise if users accidentally delete a repository or need to restore it. This is because the dashboard lacks a visible option like a recycle bin for quick restoration.

In this tutorial, we’ll learn how to use the Azure DevOps REST API to find a list of deleted repositories and recover them by making API endpoint requests using the Azure CLI and Postman.

2. Generate Token in Azure DevOps

To make the API endpoint requests, let’s create a Personal Access Token (PAT) for authentication. For that purpose, we go to the Azure DevOps portal, navigate to User settings > Personal access tokens, and click the New Token button:

Navigating to token section

After that, a window appears, requesting details, such as a name, organization, expiration date, and scopes.

Let’s make sure to select the Organization where the deleted repository is located and assign full access to the Scope:

Creating token in Azure DevOps portal

Finally, we create the token and securely store it:

Copying token

Now, let’s proceed with recovering the deleted repository.

3. Using Azure CLI

Before we go further, we log in to Azure DevOps from the Azure CLI:

$ az devops login --organization https://dev.azure.com/{organization}/
Token: 

In this command, let’s replace {organization} with the actual organization name with the deleted repository. Upon invoking the command, it prompts for the token necessary for authentication.

Once we log in, let’s make a GET request to retrieve the list of all deleted repositories in a target project. We do this by replacing the {project} with the actual project name:

$ az devops invoke --area git --resource recycleBinRepositories --route-parameters "project={project}" --http-method GET --api-version 7.1-preview
{
  "continuation_token": null,
  "count": 1,
  "value": [
    { 
...
      "deletedDate": "2024-08-15T10:00:19.707Z",
      "id": "9328c878-d5c0-443a-9821-8666eddffcd8",
      "name": "deleted-repository",
...
      }
    }
  ]
}

In the output, we should identify the deleted repository by its name and record its corresponding ID. Once we get the ID, we can create a file named payload.json to prepare for the recovery request:

{
    "deleted":false
}

After creating and saving the payload.json file, we use the Azure CLI to make a PATCH request to the API endpoint. Before that, let’s ensure that we replace the {project} with the actual project name and the {repositoryId} with the ID of the deleted repository that we recorded earlier:

$ az devops invoke --area git --resource recycleBinRepositories --route-parameters "project={project}" "repositoryId={repositoryId}" --http-method PATCH --in-file payload.json --api-version 7.1-preview
{
...
  "name": "deleted-repository",
  "project": {
    "id": "86850bb7-e2c3-48c9-882a-977361b2fe1b",
    "lastUpdateTime": "2024-08-15T03:05:02.637Z",
    "name": "demo-project",
    "revision": 11,
    "state": "wellFormed",
    "url": "https://dev.azure.com/workfor****/_apis/projects/86850bb7-e2c3-48c9-882a-977361b2fe1b",
    "visibility": "private"
  },
...
}

Finally, let’s confirm Azure DevOps has restored the repository to the Git section by checking if the output shows the state as wellFormed.

4. Using Postman

Now, we open Postman and create a new HTTP request. In particular, we make a GET request to the API endpoint at https://dev.azure.com/{organization}/{project}/\_apis/git/deletedrepositories?api-version=5.1-preview.1, using Basic Auth for authentication with the token as the password and leaving the username empty:

Listing deleted repositories in Azure DevOps

The JSON output provides a list of deleted repository names along with their unique IDs. It’s important to identify the name of the deleted repository and keep track of its corresponding ID value.

To recover the deleted repository, let’s make a PATCH request to https://dev.azure.com/{organization}/{project}/\_apis/git/recycleBin/repositories/{repositoryId}?api-version=5.1-preview.1, setting up the Authentication tab as before, with the only exception being that the Body tab of the request should be configured as raw JSON with the provided JSON payload:

{
    "deleted":false
}

After we send the request, Azure DevOps restores the deleted repository to its portal:

Recovering deleted repository in Azure DevOps

Once we recover the repository, we can revoke the personal access token, as it’s no longer required.

5. Conclusion

In this article, we discussed how to list and recover a deleted Azure DevOps repository using the Azure DevOps REST API.

First, we learned how to create a Personal Access Token (PAT) for authentication from the Azure DevOps portal. After that, we used the Azure CLI to list and recover deleted repositories, including making API requests with Postman for repository recovery.