Cleanup traits ensure your ECS never contains dangling references when entities used as tags, components, relationships, or targets are deleted. They let you specify what action to take under what condition.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Ukendio/jecs/llms.txt
Use this file to discover all available pages before exploring further.
Why Cleanup Traits?
When you delete an entity that’s used elsewhere (e.g., a component, a relationship target, or a parent), jecs needs to know what to do with all references to that entity. Cleanup traits provide this guarantee:No cleanup policy allows dangling references. Your ECS stays consistent no matter what you delete.
Per-Relationship Configuration
Different relationships need different cleanup behaviors:- Delete a tag → Remove it from all entities that have it
- Delete a parent → Delete all children
- Delete a player → Remove ownership relationships, but don’t delete the items
Cleanup Structure
Add a(Condition, Action) pair to an entity to configure its cleanup policy:
Cleanup Conditions
jecs.OnDelete: The component/tag/relationship itself is deletedjecs.OnDeleteTarget: A target used with the relationship is deleted
Cleanup Actions
jecs.Remove(default): Removes the id from all entitiesjecs.Delete: Deletes all entities with the id
If no cleanup policy is specified, the default action is
Remove.(OnDelete, Remove) - Safe Cleanup
When the component is deleted, remove it from all entities that have it.
Example
(OnDelete, Delete) - Cascading Deletion
When the component is deleted, delete all entities that have it.
Example
(OnDeleteTarget, Remove) - Safe Relationship Cleanup
When a target is deleted, remove the relationship from all entities.
Example
(OnDeleteTarget, Delete) - Hierarchical Deletion
When a target is deleted, delete all entities with that relationship.
Example: ChildOf
The built-injecs.ChildOf uses this pattern:
When a parent and children are deleted, children’s
OnRemove hooks fire before the parent’s. See Hooks & Signals for details.Comparison Table
| Trait | When Triggered | Action |
|---|---|---|
(OnDelete, Remove) | Component deleted | Remove component from all entities |
(OnDelete, Delete) | Component deleted | Delete all entities with component |
(OnDeleteTarget, Remove) | Target deleted | Remove relationship from all entities |
(OnDeleteTarget, Delete) | Target deleted | Delete all entities with relationship |
Default Behavior
If you don’t specify a cleanup trait, the default is(OnDelete, Remove):
Archetype Considerations
Cleanup operations can create and delete archetypes:- When a component is deleted, all archetypes with that component are deleted
- When relationships use entities as targets, more archetypes are created on the fly
- Extensive relationship use → more archetype creation → slightly more overhead
Complete Example
Best Practices
Use
(OnDeleteTarget, Remove) for most relationshipsSafe default that prevents dangling references without cascading deletion.Use
(OnDeleteTarget, Delete) for strict hierarchiesParent-child relationships where children shouldn’t outlive parents.Next Steps
Relationships
Learn about entity relationships and hierarchies
Hooks & Signals
React to component lifecycle events