Apply Retention Policy Task fails with Null Ref Exception

I am running Octopus Deploy v Every time the retension policy task runs it fails with the following error:

Value cannot be null.
Parameter name: key
System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer)
   at Octopus.Server.Communications.Lifecycles.LifecycleProgression.GetRelevantDeploymentsByEnvironment(IEnumerable1 deployments) in y:\work\refs\heads\release\source\Octopus.Server\Communications\Lifecycles\LifecycleProgression.cs:line 175
   at Octopus.Server.Communications.Lifecycles.LifecycleProgression.Evaluate(Boolean releaseHasUnresolvedDefect, IList1 deployments, List1 allEnvironments, Lifecycle lifecycle) in y:\work\refs\heads\release\source\Octopus.Server\Communications\Lifecycles\LifecycleProgression.cs:line 43
   at Octopus.Server.Orchestration.Retention.RetentionPolicyApplier.Receive(ApplyNextRetentionPolicyBatchCommand command) in y:\work\refs\heads\release\source\Octopus.Server\Orchestration\Retention\RetentionPolicyApplier.cs:line 134
   at Pipefish.Actor.OnReceivingTyped[TBody](Message message) in y:\work\3cbe05672d69a231\source\Pipefish\Actor.cs:line 113

The attached screen shot is from the diagnostic page.

Any thoughts or ways to debug this?

Hi Scott,

Thanks for getting in touch and I’m sorry about the issues you are having with the retention policy.

From the looks of things, there seems that you have a deployment that for some reason have no environment set (not sure how this would have happened but a few versions back we had a bug that allowed deleting things that still had references to it, so this may be the cause of this).

I have put together a powershell script that you can execute in the Script Console that hopefully can shed some light at what is causing this issue.

$apiKey = "API-xxxxxxxxxxxxxxxxxxxxxx"
$uri = "http://url/to/octopus/api/deployments"
$deployments = Invoke-RestMethod -Method GET -Uri $uri -Header @{ "X-Octopus-ApiKey" = $apiKey }

$deployments.Items | `
? { ($_.EnvironmentId -eq $null) -or ($_.EnvironmentId -eq "") } | `
% { Write-Output $_ }

If you could run that and let us know if anything is returned.

Thank you and warm regards,

Nothing was returned when we ran the script. It exited with 0. We could not find any deployments looking at the return that had an empty environment.


We found the problem. We looked at Raven DB and saw the items that didn’t have the key and deleted them (they were old deployments).


Hi Scott,

Good to hear that you found the issue and that you could get the deployments causing it removed.

Thank you and warm regards,