7

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.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
sns
  • 131
  • 1
  • 8

1 Answers1

16

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.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • Hi Maytham Fahmi, How do we get Repo GUID? – sns Aug 14 '22 at 13:51
  • 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? – sns Aug 14 '22 at 14:30
  • Please suggest how to do that? – sns Aug 14 '22 at 14:49
  • I am hearing postman first time, and I have never used – sns Aug 14 '22 at 15:00
  • 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. https://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 deleted – Maytham Fahmi Aug 14 '22 at 15:14
  • It is not working for me, can you please check my first thread where I updated screenshot. – sns Aug 14 '22 at 15:38
  • All right, Thank you, Looking forword to hear from you. – sns Aug 14 '22 at 16:51
  • 1
    HI Maytham Fahmi, Thank you very much it worked. Looking forword to work with you – sns Aug 15 '22 at 07:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247274/discussion-between-sns-and-maytham-fahmi). – sns Aug 15 '22 at 07:40
  • 1
    This was so helpful, thank you :) – 4hbane Aug 15 '22 at 10:58
  • @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? – F4-Z4 Jan 19 '23 at 11:43
  • 1
    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-preview – F4-Z4 Jan 19 '23 at 11:47
  • @F4-Z4 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. – Maytham Fahmi Jan 19 '23 at 12:17