Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lethek/f8ebedd313f3d086b50364447108c103 to your computer and use it in GitHub Desktop.
Save lethek/f8ebedd313f3d086b50364447108c103 to your computer and use it in GitHub Desktop.
Deletes all externally sourced NuGet packages from an Azure DevOps Artifacts feed
param(
[Parameter(Mandatory=$True, Position=0)]
[System.String]
$organization,
[Parameter(Mandatory=$True, Position=1)]
[System.String]
$feedName,
[Parameter(Mandatory=$True, Position=2)]
[System.String]
$connectionToken
)
$apiVersion = "7.1-preview.1"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$FeedsUrl = "https://feeds.dev.azure.com/$organization/_apis/Packaging/Feeds?api-version=$apiVersion"
$Feeds = (Invoke-RestMethod -Uri $FeedsUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$feedId = $($Feeds | Where-Object {$_.value.name -eq $feedName} | Select-Object -First 1).value.id
if ($null -ne $feedId) {
$PackagesUrl = "https://feeds.dev.azure.com/$organization/_apis/Packaging/Feeds/$feedId/Packages?api-version=$apiVersion"
$PackagesInFeed = (Invoke-RestMethod -Uri $PackagesUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$packagesId = $PackagesInFeed.value.id
ForEach ($PI in $packagesId) {
$PackageNameUrl = "https://feeds.dev.azure.com/$organization/_apis/Packaging/Feeds/$feedId/Packages/$($PI)?api-version=$apiVersion"
$PackageNames = (Invoke-RestMethod -Uri $PackageNameUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$UpstreamSource = $PackageNames.versions.directUpstreamSourceId
if ($null -ne $UpstreamSource) {
$PackageVersionUrl = "https://feeds.dev.azure.com/$organization/_apis/Packaging/Feeds/$feedId/Packages/$($PI)/Versions?api-version=$apiVersion"
$PackageVersions = (Invoke-RestMethod -Uri $PackageVersionUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$VersionsId = $PackageVersions.value.version
$packageName = $PackageNames.name
Write-Host "Deleting Package: $packageName"
Write-Host "Versions: $($VersionsId -join ", ")"
Write-Host
ForEach ($VI in $VersionsId) {
$DeleteUrl = "https://pkgs.dev.azure.com/$organization/_apis/Packaging/Feeds/$feedId/nuget/Packages/$($packageName)/Versions/$($VI)?api-version=$apiVersion"
$Member = (Invoke-RestMethod -Uri $DeleteUrl -Method DELETE -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
}
} elseif ($targetTask.result -eq "Failed") {
Write-Host ("This is package not from upstream source!")
}
}
} else {
Write-Host "Feed '$feedName' could not be found!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment