How to remove a package from nuget.org

I wanted to remove/unlist one of my packages from nuget.org. It was however not a trivial task to perform as ALL package versions must be unlisted. This article will show you a programmatic way to do so.

Start by creating a new console application in Visual Studio and install the nuget package “nuget.core”. With that package you can download a list of all package versions of your package. The code for that method should look something like this:

private static IEnumerable<IPackage> GetListedPackages(string packageID)
{
    var repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
    var packages = from package in repo.FindPackagesById(packageID)
        where package.IsListed()
        select package;
    return packages;
}

The problem is that the “Nuget.Core” library to not support the remove action. So you can get a nice list of all versions that have to be removed. But you can’t remove them.

My approach was to copy nuget.exe from the “.nuget” folder in one of my projects and include it in the console application:

unlist all project

Once that is done it’s rather trivial. Set “Copy to Output Directory” to “Copy if newer” in the file properties.

Now we can run the “nuget remove” command from our code:

private static string UnlistPackage(IPackage package, string apiKey)
{
    var arguments = $"delete {package.Id} {package.Version} -ApiKey {apiKey} -NonInteractive";
    var psi = new ProcessStartInfo("nuget.exe", arguments)
    {
        RedirectStandardOutput = true,
        WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
        UseShellExecute = false
    };
    var process = Process.Start(psi);
    process.WaitForExit();
    return process.StandardOutput.ReadToEnd();
}

Notice that I copy the output from the command to verify that it was actually run.

The last thing that we need is the code that ties everything together:

private static void Main(string[] args)
{
    var packageID = "yourPackageName";
    var apiKey = "yourKeyFromNugetOrg";

    var packages = GetListedPackages(packageID);

    foreach (var package in packages)
    {
        Console.WriteLine("Unlisting package " + package);
        var output = UnlistPackage(package, apiKey);
        Console.WriteLine(output);
    }

    Console.Write("Completed. Press ENTER to quit.");
    Console.ReadLine();
}

  • Not really a good idea to act in this way – relying on the command line is flaky.
    The best way, in my opinion, would be to hit their WebAPI directly.

    • Why is it flaky? What do you expect will happen?

      • 1. It’s not portable (try that on an Unix system)
        2. You’re checking in a binary dependency in your project
        3. It’s impossible to test the command
        4. If the WebAPI changes and you do not change the .exe promptly, it will crash

        My point is – do not rely on the shell from a program. If you really want, then write a program whose output is a list of packages and pipe it to a script which is calling nuget.exe.
        At least you set right expectations (it’s a script, so it’s OS dependant and so on)

        • Will you ever distribute that code in an application? I find it highly unlikely. It’s a small tool which you use yourself if you ever get into that problem.

          Come one, how often will you have the need to unlist all versions of a nuget package?

          As such, non of your issues are relevant.