How to recover deleted repository in Azure DevOps?
Asked Answered
S

1

10

I deleted Azure Repo by mistake, now I want to restore that repo is there any way we can restore it?

If possible, please advise.

Strident answered 14/8, 2022 at 11:25 Comment(0)
T
22

If you have deleted your repository, it will stay in Azure DevOps recycle bin for 30 days; so restoration is possible but you need to follow some manual steps as shown below.

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

  1. Create a PAT (Personal Access Token) in Azure DevOps.

  2. You will need to use a tool such as a Postman software to make some API requests; using that, create a get request using this URL: https://dev.azure.com/{ORGANIZATION_NAME}/{PROJECT_NAME}/_apis/git/deletedrepositories?api-version=5.1-preview.1

  3. Make Basic Auth, put the Token as a password, and leave the username empty.

  4. The response contains the id of your deleted repo (a GUID id).

enter image description here So making the request step 2, you get a JSON payload containing the REPO GUID as highlighted in yellow. That you need to copy for step 5.

  1. Make another Patch request to following URL: https://dev.azure.com/{ORGANIZATION_NAME}/{PROJECT_NAME}/_apis/git/recycleBin/repositories/{REPO_GUID}?api-version=5.1-preview.1

With the following JSON payload body:

{
    "deleted":false
}

When you make this request, it will recover the deleted repository from recycle bin.

Note: Replace {ORGANIZATION_NAME}, {PROJECT_NAME} and {REPO_GUID} with the right values.

I have tested this way and it works.

Disclaimer: This answer knowledge is based on an article I wrote in my personal blog. It is also possible to use this tool (AzdoTools) to recover the repositories. more info about it is in the given article link.

Tarkany answered 14/8, 2022 at 11:32 Comment(14)
Hi Maytham Fahmi, How do we get Repo GUID?Strident
HI Maytham, Where I should provide the url you have mentioned in the second step? what tool it is ? can you please provide more information on this?Strident
Please suggest how to do that?Strident
I am hearing postman first time, and I have never usedStrident
I have developed this tool, it is not public yet, but you can download this tool that does exactly the same steps as the above answer. itbackyard.com/others/download/AzdoTools.zip unzip it then run it, you need to run it like AzdoTools.exe -t xxtokenxx -p xxProjectNamexx -o xxOrgNamexx it will show a list of deleted repositories for the particular project then you can select number 1 to recover or number 2, depending on how many repositories are deletedTarkany
It is not working for me, can you please check my first thread where I updated screenshot.Strident
All right, Thank you, Looking forword to hear from you.Strident
HI Maytham Fahmi, Thank you very much it worked. Looking forword to work with youStrident
Let us continue this discussion in chat.Strident
This was so helpful, thank you :)Coxa
@MaythamFahmi where have you found the retention policy is 30 days? As far as I know, Microsoft only mentions “a period of time” in their Azure DevOps Services API documentation. Or, is this based on your observation?Attainder
You can use Azure CLI with DevOps to call API of Azure DevOps Services including this case. This way you can rely on whatever authentication mechanism Azure CLI supports (e.g., az login). The command looks like this: az devops invoke --area git --resource recycleBinRepositories --route-parameters "project=PROJECT_ID_OR_NAME" "repositoryId=REPOSITORY_ID" --http-method PATCH --in-file deleted-false.json --api-version 7.1-previewAttainder
@Attainder regarding 30 days honestly I do not remember the source but I have tried with in 30 days on Azure DevOps service. Of course it is possible using azure cli, end of the day it is about making a post request. I have made a tool called azdotools you can try it, it will serve the same manner. Enjoy your day.Tarkany
Just used AzdoTools with great success. 1000 thanks!Katharynkathe

© 2022 - 2024 — McMap. All rights reserved.