sschmid/Entitas · error · ContextDoesNotContainEntityException

' ' cannot destroy !

Error message

'{this}' cannot destroy {tEntity}!

What it means

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.

Solutions

  1. Verify context.HasEntity(entity) before calling DestroyEntity
  2. Destroy entities obtained only from this same context instance
  3. Track destroyed entities and drop stale references after Reset or DestroyAllEntities

Example fix

// before
staleEntity.Destroy(); // may throw if not in context
// after
if (context.HasEntity(staleEntity)) staleEntity.Destroy();
Defensive patterns

Strategy: type-guard

Validate before calling

if (context.HasEntity(entity)) entity.Destroy();

Type guard

bool IsDestroyable(TEntity e) => context.HasEntity(e);

Prevention

When it happens

Trigger: 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.

Common situations: Cross-context entity sharing, holding stale entity references after Reset/DestroyAllEntities and calling Destroy on them, or double-destroy in teardown code.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of sschmid/Entitas@37547d1bd2 (2026-09-14). Data as JSON: /api/errors/d51293ec45293a1c. Report an issue: GitHub.

Appendix: source

Thrown at src/Entitas/Context/Context.cs:157

            };

            _OnEntityReleasedDelegate = entity =>
            {
                if (entity.IsEnabled)
                    throw new EntityIsNotDestroyedException($"Cannot release {entity}!");

                var tEntity = (TEntity)entity;
                entity.RemoveAllOnEntityReleasedHandlers();
                _retainedEntities.Remove(tEntity);
                _reusableEntities.Push(tEntity);
            };

            _OnDestroyEntityDelegate = entity =>
            {
                var tEntity = (TEntity)entity;
                var removed = _entities.Remove(tEntity);
                if (!removed)
                    throw new ContextDoesNotContainEntityException(
                        $"'{this}' cannot destroy {tEntity}!",
                        "This cannot happen!?!"
                    );

                _entitiesCache = null;

                OnEntityWillBeDestroyed?.Invoke(this, tEntity);
                tEntity.InternalDestroy();
                OnEntityDestroyed?.Invoke(this, tEntity);

                if (tEntity.RetainCount == 1)
                {
                    // Can be released immediately without
                    // adding to _retainedEntities
                    tEntity.OnEntityReleased -= _OnEntityReleasedDelegate;
                    _reusableEntities.Push(tEntity);
                    tEntity.Release(this);
                    tEntity.RemoveAllOnEntityReleasedHandlers();

View on GitHub (pinned to 37547d1bd2)