{"record":{"id":"d51293ec45293a1c","repo":"sschmid/Entitas","slug":"this-cannot-destroy-tentity","errorCode":null,"errorMessage":"'{this}' cannot destroy {tEntity}!","messagePattern":"'(.+?)' cannot destroy (.+?)!","errorType":"exception","errorClass":"ContextDoesNotContainEntityException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Context/Context.cs","lineNumber":157,"sourceCode":"            };\n\n            _OnEntityReleasedDelegate = entity =>\n            {\n                if (entity.IsEnabled)\n                    throw new EntityIsNotDestroyedException($\"Cannot release {entity}!\");\n\n                var tEntity = (TEntity)entity;\n                entity.RemoveAllOnEntityReleasedHandlers();\n                _retainedEntities.Remove(tEntity);\n                _reusableEntities.Push(tEntity);\n            };\n\n            _OnDestroyEntityDelegate = entity =>\n            {\n                var tEntity = (TEntity)entity;\n                var removed = _entities.Remove(tEntity);\n                if (!removed)\n                    throw new ContextDoesNotContainEntityException(\n                        $\"'{this}' cannot destroy {tEntity}!\",\n                        \"This cannot happen!?!\"\n                    );\n\n                _entitiesCache = null;\n\n                OnEntityWillBeDestroyed?.Invoke(this, tEntity);\n                tEntity.InternalDestroy();\n                OnEntityDestroyed?.Invoke(this, tEntity);\n\n                if (tEntity.RetainCount == 1)\n                {\n                    // Can be released immediately without\n                    // adding to _retainedEntities\n                    tEntity.OnEntityReleased -= _OnEntityReleasedDelegate;\n                    _reusableEntities.Push(tEntity);\n                    tEntity.Release(this);\n                    tEntity.RemoveAllOnEntityReleasedHandlers();","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Context/Context.cs#L139-L175","documentation":"Context.DestroyEntity removes the entity from the context's internal entity set; if Remove returns false the context did not contain the entity. Entitas labels this 'This cannot happen!?' because it indicates a corrupted internal invariant rather than normal misuse.","triggerScenarios":"Destroying an entity that is not tracked by this context — e.g. an entity from another context, an already-destroyed/recycled entity instance, or entity objects obtained via unsafe means bypassing HasEntity.","commonSituations":"Cross-context entity sharing, holding stale entity references after Reset/DestroyAllEntities and calling Destroy on them, or double-destroy in teardown code.","solutions":["Verify context.HasEntity(entity) before calling DestroyEntity","Destroy entities obtained only from this same context instance","Track destroyed entities and drop stale references after Reset or DestroyAllEntities"],"exampleFix":"// before\nstaleEntity.Destroy(); // may throw if not in context\n// after\nif (context.HasEntity(staleEntity)) staleEntity.Destroy();","handlingStrategy":"type-guard","validationCode":"if (context.HasEntity(entity)) entity.Destroy();","typeGuard":"bool IsDestroyable(TEntity e) => context.HasEntity(e);","tryCatchPattern":null,"preventionTips":["Never keep entity references across context Reset","Destroy only entities from the owning context","Drop stale references after DestroyAllEntities"],"tags":["entitas","context","entity","invariant"],"backgroundTag":"entity-not-found","analyzedSha":"37547d1bd280f3f1c1ef7edb44f2a206660fbfb4","analyzedAt":"2026-09-14T00:26:23.246Z","contentChangedAt":"2026-09-14T00:26:23.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}