Recover a deleted VSTS git repo

VSTS provide a way to restore a deleted git branch from the web portal, but what if you accidentally deleted a repo?

There is no way from VSTS web portal to restore a deleted repo, but two nice REST APIs are out there for you to manage soft-deleted git repo.

Recently deleted repositories go into a soft-delete state for a period of time before they are hard deleted and become unrecoverable.

  1. Get Recycle Bin Repositories: Retrieve soft-deleted git repositories from the recycle bin.
  2. Restore Repository From Recycle Bin: Recover a soft-deleted Git repository.

Get Recycle Bin Repositories

I have recently deleted a repo I created “RepoToBeDeletedAndRecovered” and other 2 repos. When hitting the REST API, you will get a list of soft-deleted repos.

GET https://{accountName}.visualstudio.com/{project}/_apis/git/recycleBin/repositories?api-version=4.1-preview.1
Get Recycle Bin Repositories- Sample Request/Response

Restore Repository From Recycle Bin

Use the repo id to issue a PATCH request to restore the repo by changing the “deleted” field to false.

PATCH https://{accountName}.visualstudio.com/{project}/_apis/git/recycleBin/repositories/{repositoryId}?api-version=4.1-preview.1
Restore Repository From Recycle Bin- Sample Request/Response

In VSTS, the repo is back!

Finally,

It may be a good practices to modify the default permissions to disable “Delete Repository” permissions, you can set it at the top-level Git repositories.