Skip to content

Instantly share code, notes, and snippets.

@asbjornu
Forked from dazinator/unlist-packages.ps1
Last active June 10, 2020 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asbjornu/b31ca996a40a54f18bca01fff0335608 to your computer and use it in GitHub Desktop.
Save asbjornu/b31ca996a40a54f18bca01fff0335608 to your computer and use it in GitHub Desktop.
Unlist all versions of a NuGet package
$PackageId = "xxx"
$ApiKey = "yyy"
$json = Invoke-WebRequest -Uri "https://api.nuget.org/v3-flatcontainer/$PackageId/index.json" | ConvertFrom-Json
foreach ($version in $json.versions)
{
Write-Host "Unlisting $PackageId, Ver $version"
Invoke-Expression "dotnet nuget delete $PackageId $version --non-interactive --api-key $ApiKey --source https://api.nuget.org/v3/index.json"
}
#!/bin/bash
set -o errexit #abort if any command fails
[ "${DEBUG:-false}" = "true" ] && set -x
me=$(basename "$0")
help_message="\
Usage: $me <package-id> <api-key>
Deletes (unlists) the <package-id> from nuget.org.
Arguments:
package-id The ID of the package you want to delete.
api-key The nuget.org API key with access to delete the package."
package_id="$1"
api_key="$2"
if [[ -z "$package_id" ]]; then
echo "Missing argument: package-id. Aborting." >&2
echo "$help_message" >&2
exit 1
fi
if [[ -z "$api_key" ]]; then
echo "Missing argument: api-key. Aborting." >&2
echo "$help_message" >&2
exit 1
fi
json=$(wget --quiet --output-document - "https://api.nuget.org/v3-flatcontainer/$package_id/index.json" 2> /dev/null)
count=$(echo "$json" | jq -r '.versions | length')
versions=$(echo "$json" | jq -r '.versions[]')
echo "Unlisting $count versions of $package_id..."
echo ""
for version in $versions; do
echo "Unlisting $package_id, version $version."
dotnet nuget delete "$package_id" "$version" --non-interactive --api-key "$api_key" --source "https://api.nuget.org/v3/index.json"
echo ""
# Sleep for 5 seconds to avoid exceeding NuGet's rate limits:
# https://docs.microsoft.com/nb-no/nuget/api/rate-limits#package-push-and-unlist
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment