Table of Contents

Проблема

на локальный сервер Nuget загрузили кривую версию пакета.
Надо удалить ее оттуда, а лучше - заменить на исправленную.

Решение

Строго говоря - серверы Nuget не разрешают заменять уже загруженные версии пакетов, а предполагает, что исправленные пакеты будут заливаться с новыми версиями - https://docs.microsoft.com/en-us/azure/devops/artifacts/artifacts-key-concepts?view=azure-devops
Даже если пакет был удален с помощью nuget delete - он будет все равно доступен для скачивания, хотя и не будет отображаться в интерфейсе.
Однако, есть хирургический способ обойти это ограничение - удалить пакет (все его версии) в БД сервера Nuget. Или вот - более продвинутые варианты скриптов, удаляющие пакеты по маске, или конкретные версии

Скрипт для удаления всех версий заданного пакета nuget

DECLARE @PackageRegistrationKey int
SELECT @PackageRegistrationKey = [Key]
 FROM PackageRegistrations
 WHERE Id = 'MyNastyPackage'

BEGIN TRANSACTION
DELETE pf
 FROM [NuGetGallery].[dbo].[PackageFrameworks] pf
 JOIN Packages p ON pf.Package_Key = p.[Key]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE pa
 FROM [NuGetGallery].[dbo].[PackageAuthors] pa
 JOIN Packages p ON pa.PackageKey = p.[Key]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE gs
 FROM [NuGetGallery].[dbo].[GallerySettings] gs
 JOIN [PackageStatistics] ps ON gs.DownloadStatsLastAggregatedId = ps.[Key]
 JOIN Packages p ON ps.PackageKey = p.[Key]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE ps
 FROM [NuGetGallery].[dbo].[PackageStatistics] ps
 JOIN Packages p ON ps.PackageKey = p.[Key]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE pd
 FROM [NuGetGallery].[dbo].[PackageDependencies] pd
 JOIN Packages p ON pd.PackageKey = p.[Key]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE
 FROM [NuGetGallery].[dbo].[Packages]
 WHERE PackageRegistrationKey = @PackageRegistrationKey
DELETE por
 FROM PackageOwnerRequests por
 JOIN PackageRegistrations pr ON pr.[Key] = por.PackageRegistrationKey
 WHERE pr.[Key] = @PackageRegistrationKey
DELETE pro
 FROM PackageRegistrationOwners pro
 JOIN PackageRegistrations pr ON pr.[Key] = pro.PackageRegistrationKey
 WHERE pr.[Key] = @PackageRegistrationKey
DELETE FROM PackageRegistrations
 WHERE [Key] = @PackageRegistrationKey
COMMIT TRANSACTION

Скрипт для удаления пакетов nuget, имена которых начинаются с заданной строки

-- Delete packages with ids that start with a specific string
DECLARE @PackageRegistrationKeys table([Key] int)
INSERT INTO @PackageRegistrationKeys
SELECT [Key] FROM [NuGetGallery].[dbo].[PackageRegistrations]
WHERE [Id] LIKE 'Example.Nasty.Package.%'

BEGIN TRANSACTION
DELETE pf
 FROM [NuGetGallery].[dbo].[PackageFrameworks] pf
 JOIN [NuGetGallery].[dbo].[Packages] p ON pf.[Package_Key] = p.[Key]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE pa
 FROM [NuGetGallery].[dbo].[PackageAuthors] pa
 JOIN [NuGetGallery].[dbo].[Packages] p ON pa.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE gs
 FROM [NuGetGallery].[dbo].[GallerySettings] gs
 JOIN [NuGetGallery].[dbo].[PackageStatistics] ps ON gs.[DownloadStatsLastAggregatedId] = ps.[Key]
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE ps
 FROM [NuGetGallery].[dbo].[PackageStatistics] ps
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE pd
 FROM [NuGetGallery].[dbo].[PackageDependencies] pd
 JOIN [NuGetGallery].[dbo].[Packages] p ON pd.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE
 FROM [NuGetGallery].[dbo].[Packages]
 WHERE [PackageRegistrationKey] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE por
 FROM [NuGetGallery].[dbo].[PackageOwnerRequests] por
 JOIN [NuGetGallery].[dbo].[PackageRegistrations] pr ON pr.[Key] = por.[PackageRegistrationKey]
 WHERE pr.[Key] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE pro
 FROM [NuGetGallery].[dbo].[PackageRegistrationOwners] pro
 JOIN [NuGetGallery].[dbo].[PackageRegistrations] pr ON pr.[Key] = pro.[PackageRegistrationKey]
 WHERE pr.[Key] IN (SELECT [Key] FROM @PackageRegistrationKeys)
DELETE FROM [NuGetGallery].[dbo].[PackageRegistrations]
 WHERE [Key] IN (SELECT [Key] FROM @PackageRegistrationKeys)
COMMIT TRANSACTION

Скрипт для удаления конкретной версии пакета nuget

-- Delete a specific version of a package with a specific package id
DECLARE @PackageKey int
SELECT @PackageKey = p.[Key]
 FROM [Packages] p
 JOIN [PackageRegistrations] pr ON pr.[Key] = p.[PackageRegistrationKey]
 WHERE pr.[Id] = 'Example.Nasty.Package'
 AND [Version] = '9.9.9'

BEGIN TRANSACTION
DELETE pf
 FROM [NuGetGallery].[dbo].[PackageFrameworks] pf
 JOIN [NuGetGallery].[dbo].[Packages] p ON pf.[Package_Key] = p.[Key]
 WHERE p.[Key] = @PackageKey
DELETE pa
 FROM [NuGetGallery].[dbo].[PackageAuthors] pa
 JOIN [NuGetGallery].[dbo].[Packages] p ON pa.[PackageKey] = p.[Key]
 WHERE p.[Key] = @PackageKey
DELETE gs
 FROM [NuGetGallery].[dbo].[GallerySettings] gs
 JOIN [NuGetGallery].[dbo].[PackageStatistics] ps ON gs.[DownloadStatsLastAggregatedId] = ps.[Key]
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE p.[Key] = @PackageKey
DELETE ps
 FROM [NuGetGallery].[dbo].[PackageStatistics] ps
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE p.[Key] = @PackageKey
DELETE pd
 FROM [NuGetGallery].[dbo].[PackageDependencies] pd
 JOIN [NuGetGallery].[dbo].[Packages] p ON pd.[PackageKey] = p.[Key]
 WHERE p.[Key] = @PackageKey
DELETE
 FROM [NuGetGallery].[dbo].[Packages]
 WHERE [Key] = @PackageKey
COMMIT TRANSACTION

Скрипт для удаления всех версий пакета nuget

-- Delete all versions of a package with a specific package id
DECLARE @PackageRegistrationKey int
SELECT @PackageRegistrationKey = [Key]
 FROM [PackageRegistrations]
 WHERE [Id] = 'Example.Nasty.Package'

BEGIN TRANSACTION
DELETE pf
 FROM [NuGetGallery].[dbo].[PackageFrameworks] pf
 JOIN [NuGetGallery].[dbo].[Packages] p ON pf.[Package_Key] = p.[Key]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE pa
 FROM [NuGetGallery].[dbo].[PackageAuthors] pa
 JOIN [NuGetGallery].[dbo].[Packages] p ON pa.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE gs
 FROM [NuGetGallery].[dbo].[GallerySettings] gs
 JOIN [NuGetGallery].[dbo].[PackageStatistics] ps ON gs.[DownloadStatsLastAggregatedId] = ps.[Key]
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE ps
 FROM [NuGetGallery].[dbo].[PackageStatistics] ps
 JOIN [NuGetGallery].[dbo].[Packages] p ON ps.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE pd
 FROM [NuGetGallery].[dbo].[PackageDependencies] pd
 JOIN [NuGetGallery].[dbo].[Packages] p ON pd.[PackageKey] = p.[Key]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE
 FROM [NuGetGallery].[dbo].[Packages]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE por
 FROM [NuGetGallery].[dbo].[PackageOwnerRequests] por
 JOIN [NuGetGallery].[dbo].[PackageRegistrations] pr ON pr.[Key] = por.[PackageRegistrationKey]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE pro
 FROM [NuGetGallery].[dbo].[PackageRegistrationOwners] pro
 JOIN [NuGetGallery].[dbo].[PackageRegistrations] pr ON pr.[Key] = pro.[PackageRegistrationKey]
 WHERE [PackageRegistrationKey] = @PackageRegistrationKey
DELETE FROM [NuGetGallery].[dbo].[PackageRegistrations]
 WHERE [Key] = @PackageRegistrationKey
COMMIT TRANSACTION