Deleting Tenant gives error "Sequence contains no elements"

I’m trying to delete a tenant on my Octopus Server, and every time I do, I get the error “Sequence contains no elements”. I’ve made sure that there are no variable sets for the tenant, no infrastructure, no projects, and no stored releases with the tenant, but deletion always fails. I’m currently running v2020.3.4.

Here’s the detailed exception I found in the server log:
Exception has been thrown by the target of an invocation.
System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.Aggregate[TSource](IEnumerable1 source, Func3 func)
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableRestriction.<>c__DisplayClass10_0.g__HasPermissionWhenOwnerIsLibraryVariableSet|3(Tenant tenant) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 254
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableRestriction.<>c__DisplayClass10_0.b__4() in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 233
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableOwnerProvider.UsingProjectOwner[T](TenantVariable variable, Func1 whenOwnerIsProject, Func1 whenOwnerIsLibraryVariableSet) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 41
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableRestriction.<>c__DisplayClass10_0.g__HasOtherEditPermissions|1() in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 231
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableRestriction.HasTenantEditAndVariableEdit(TenantVariable model, ISpacePartitionPrincipal principal, IOctopusQueryExecutor queryExecutor) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 226
at Octopus.Server.Web.Api.ModelRestrictionMap.TenantVariableRestriction.CanDelete(TenantVariable model, ISpacePartitionPrincipal principal, IOctopusQueryExecutor queryExecutor) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Api\ModelRestrictionMap\TenantVariableRestrictionDefinition.cs:line 211
at Octopus.Server.Web.Infrastructure.Security.AccessChecker1.<>c__DisplayClass12_0.<CanDelete>b__2(ISpacePartitionPrincipal spacePrincipal, IOctopusQueryExecutor scopedQueryExecutor) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Infrastructure\Security\AccessChecker.cs:line 151 at Octopus.Server.Web.Infrastructure.Security.AccessChecker1.<>c__DisplayClass12_0.b__0() in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Infrastructure\Security\AccessChecker.cs:line 150
at Octopus.Server.Web.Infrastructure.Security.AccessChecker1.CanDelete(TModel model) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Infrastructure\Security\AccessChecker.cs:line 147 at Octopus.Server.Web.Infrastructure.Security.DocumentRestrictions.AccessCheckerResolver.AccessCheckerTypeAdapter2.CanDelete(TDocument model) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Server\Web\Infrastructure\Security\DocumentRestrictions\AccessCheckerResolver.cs:line 132
at Octopus.Core.Security.Permissions.AccessCheckerExtensions.AssertCanDelete[TModel](IAccessChecker1 accessChecker, TModel model) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Core\Security\Permissions\IAccessChecker.cs:line 38 at Octopus.Core.Persistence.Indexes.DeleteCommand.DeleteRecursive[TDocument](Func2 getExecutor, TDocument document, ReferenceCollection seenBefore, Boolean unrestricted) in C:\buildAgent\work\7881e50627116cb\source\Octopus.Core\Persistence\Indexes\DeleteCommand.cs:line 50

Hi Michael,

Thanks for getting in touch! I’m sorry to hear you’re hitting this unexpected issue here. The error message you’re seeing looks identical to a bug discovered just recently, which I reported here.

In short, this is due to poor handling of the process of disconnecting a tenant from a project while it has common variables defined (via library variable sets) that’s preventing the deletion of the tenant. The workaround was to connect the tenant to a project, where then the deletion is allowed to happen in the UI. Does that workaround help in your case?

I hope this helps, and let me know how you go. :slight_smile:

Best regards,

Kenny

Thanks for linking that report. I didn’t think to look over on GH for issues as well. Following the workaround fixed the issue and I was able to finally delete the “ghost” tenant.

Hi Michael,

Thanks for the update, and great to hear the workaround allowed you to delete the tenant! Please let us know if you have any questions or concerns in the future. :slight_smile:

Best regards,

Kenny