How to Recover Deleted Azure DevOps Repository?

You might possibly delete an Azure DevOps repository by mistake and want to recover it. As a standard, it is not possible to recover deleted repositories from the user interface. In this article, I will explain how to undelete a repository manually, and in addition, I will introduce a little tool that recovers your repository automatically.

Before we go into this, it is worth mentioning that if you have deleted your repository, it will stay in Azure DevOps recycle bin for 30 days; so restoration is possible. If your project is deleted, that is another scenario that I hope will take in another article when time allows.

For this reason, I created an Azure DevOps Project in my Azure DevOps Services calls it RecoverProject. By default, the main repository for the project will have the same name as the project name which is RecoverProject. I have in addition created 2 more repositories for the same project, I called them RecoverProjectA and RecoverProjectB.

After creating the repositories, I have intentionally deleted 2 repositories ReocverProject and RecoverProjectB

As you can see, only one repository is left, but 2 are deleted, let’s recover both repositories.

Recover it manually

Within 30 days, you can follow the steps below to recover your repository.

You will need to use a tool such as a Postman or curl command line to make some API requests; (in this example I will use Postman).

1- In Postman Create a GET request using this URL (replace organization_name and project_name with the right value):

https://dev.azure.com/{ORGANIZATION_NAME}/{PROJECT_NAME}/_apis/git/deletedrepositories?api-version=5.1-preview.1

2- In Authorization, set the type to Basic Auth.

3- Create a PAT (Personal Access Token) in Azure DevOps, paste the PAT token in the password field and leave the username empty. as shown in the image below.

Make the request by clicking on Send and you will get a JSON payload response like the image below. As you can see in this particular case, we have 2 deleted repositories. The 2 deleted repositories are highlighted with yellow. We copy the Id of the repositories we want to recover and let’s call each id for REPO_GUID. Copy the REPO_GUID id of each repository in the notepad that we will use in the next step.

5- In this final step, make a new PATCH request with the following URL (replace organization_name and project_name with the right value and replace REPO_GUID with the id you copied in notepad in the previous step) and I will do it twice, one of each repository I want to recover:

https://dev.azure.com/{ORGANIZATION_NAME}/{PROJECT_NAME}/_apis/git/recycleBin/repositories/{REPO_GUID}?api-version=5.1-preview.1

The PATCH request JSON body:

{
    "deleted":false
}

When I am done with both requests, it will recover the deleted repository from recycle bin.

When I go back to my Azure DevOps project UI and go to the repository overview, I can see them again as shown in the image below.

Use the tool to automatically recover deleted repos

I have created a little software, still in beta but works fine. This tool works on Windows right now, it is not public yet, but I share it on my site here, download it, and use it to recover your repository.

What I did, was I delete the same repositories again and used my tools. you still need to provide the PAT token, project, and organization name to the command line.

Example:

 .\AzdoTools.exe -t xxPATxxTokenxx -p xxProjectNamexx -o xxOrgNamexx

so for the repository that I deleted here is how I did recover them:

When I was done with the recovery, I went back to my Azure DevOps repo UI and found the recovered repositories again.

You can download the AzdoTools.exe version 1.0.2-Beta here. If you have input or feedback I will be happy to learn more from you so I can improve it before releasing it.

Btw this answer in StackOverflow is the outcome of this article.

Conclusion

As you can see, in Azure DevOps it is not possible to recover the repository from User Interface, it requires manual interaction. But the steps around are straightforward. I also used Postman, but there are other tools like Curl, mockoon, insomnia, stoplight, SoapUI, and others I might forget, it is a matter of personal preference and taste. I chose to develop this tool just to reduce the manual work of recovering the repository.

That said, it does not often happen that you delete a repo without considering your action, but it could happen for one or other reason and you might find out you need to recover stuff before 30 days.

8 thoughts on “How to Recover Deleted Azure DevOps Repository?”

  1. Do you know if there is a way to alter the length of time a deleted repo remains in the Azure DevOps recycle bin?

    Reply
    • Thank you for reaching out. As far as I know, it is set by default by the Microsoft Azure DevOps team, and not possible to change it. It is also sad to say that, there is scant information on this. If this is not correct I will update my comments/answer in the future.

      Reply

Leave a Comment