Skip to content

Instantly share code, notes, and snippets.

@mdailey77
Created April 9, 2022 12:38
Show Gist options
  • Save mdailey77/adaf4c7acf24836a9f2c3a3346a9a4da to your computer and use it in GitHub Desktop.
Save mdailey77/adaf4c7acf24836a9f2c3a3346a9a4da to your computer and use it in GitHub Desktop.
Remove all stored npm packages from Azure Artifacts
<# Azure DevOps REST API #>
$url1 = 'https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feed_id}/packages?protocolType=npm&includeAllVersions=true&api-version=7.1-preview.1'
$Token = '{Personal_Access_Token}'
if ($Token -eq "") {
Write-Host 'PAT not set'
exit 1
}
$AzureAuthHeader1 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))
$headers1 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers1.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader1))
$headers1.Add("Content-Type", "application/json")
$response1 = Invoke-RestMethod -Uri $url1 -Method GET -Headers $headers1
$packages = $response1.value
<# Loops through returned npm package versions and deletes them #>
Foreach ($i in $packages)
{
$packageName = $i.name
Write-Host $i.name -BackgroundColor cyan
$allversions = $i.versions
Foreach ($v in $allversions) {
$packageVersion = $v.version
$url2 = 'https://pkgs.dev.azure.com/{organization}/_apis/packaging/feeds/{feed_id}/npm/{0}/versions/{1}?api-version=7.1-preview.1' -f $packageName, $packageVersion
$AzureAuthHeader2 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))
$headers2 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers2.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader2))
$headers2.Add("Content-Type", "application/json")
$response2 = Invoke-RestMethod -Uri $url2 -Method DELETE -Headers $headers2
Write-Host 'deleted' + $response2.value
}
}
@mdailey77
Copy link
Author

If there are a large number of npm packages, this script will have to be run multiple times given there is a limit on the number of npm package versions retrieved at one time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment