{"record":{"id":"f079fc36055b5fdb","repo":"stride3d/stride","slug":"scenes-with-entities-cannot-be-removed","errorCode":null,"errorMessage":"Scenes with entities cannot be removed.","messagePattern":"Scenes with entities cannot be removed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Assets.Presentation/AssetEditors/SceneEditor/Game/SceneEditorGame.cs","lineNumber":91,"sourceCode":"        /// <summary>\n        /// Removes the existing scene identified by <paramref name=\"sceneId\"/>.\n        /// </summary>\n        /// <param name=\"sceneId\">The identifier of the scene to remove.</param>\n        /// <remarks>\n        /// The scene must be empty, i.e. its child scenes must have been removed first and its entities unloaded.\n        /// </remarks>\n        public void RemoveScene(Guid sceneId)\n        {\n            if (sceneId == Guid.Empty)\n                throw new InvalidOperationException($\"{nameof(sceneId)} cannot be {nameof(Guid.Empty)}.\");\n\n            EnsureContentScene();\n\n            var scene = GetScene(sceneId);\n            if (scene.Children.Count > 0)\n                throw new InvalidOperationException(\"Scenes with child scenes cannot be removed.\");\n            if (scene.Entities.Count > 0)\n                throw new InvalidOperationException(\"Scenes with entities cannot be removed.\");\n\n            SceneRemoved?.Invoke(scene);\n            RemoveSceneFromParent(scene);\n            scenes.Remove(sceneId);\n        }\n\n        /// <inheritdoc/>\n        public override Entity FindSubEntity(Guid sceneId, Guid entityId)\n        {\n            EnsureContentScene();\n\n            Scene scene;\n            if (scenes.TryGetValue(sceneId, out scene))\n            {\n                Entity entity;\n                // Note: special case of the virtual anchor (sceneID == entityId), own by the parent scene\n                if (sceneId == entityId)\n                    entity = scene.Parent?.FindSubEntity(entityId);","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Assets.Presentation/AssetEditors/SceneEditor/Game/SceneEditorGame.cs#L73-L109","documentation":"SceneEditorGame.RemoveScene throws this InvalidOperationException when the target scene still contains loaded entities. Scenes must be empty of entities before removal, otherwise loaded game-side objects would be orphaned.","triggerScenarios":"Calling RemoveScene on a scene whose Entities collection is non-empty (entities were loaded via LoadEntities and never unloaded).","commonSituations":"Deleting a scene without first unloading its entities; editor sessions where entity unloading failed or was skipped.","solutions":["Unload the scene's entities before calling RemoveScene.","Track loaded entities per scene and unload them in cleanup logic.","Catch InvalidOperationException and report that the scene must be emptied first."],"exampleFix":"// before\nsceneEditorGame.RemoveScene(sceneId); // entities still loaded\n// after\nif (sceneEditorGame.GetScene(sceneId).Entities.Count == 0)\n    sceneEditorGame.RemoveScene(sceneId);\nelse\n    UnloadSceneEntitiesThenRemove(sceneId);","handlingStrategy":"validation","validationCode":"var scene = game.GetScene(sceneId);\nif (scene == null || scene.Entities.Count > 0) UnloadEntitiesAndRetry(sceneId);","typeGuard":"bool IsEmptyScene(Guid id) { var s = game.GetScene(id); return s != null && s.Children.Count == 0 && s.Entities.Count == 0; }","tryCatchPattern":"try { game.RemoveScene(sceneId); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"entities\")) { UnloadSceneEntities(sceneId); game.RemoveScene(sceneId); }","preventionTips":["Unload entities before removing their scene","Track entity ownership per scene in editor tooling","Verify scene emptiness with GetScene before removal"],"tags":["stride-editor","scene","entities","invalid-state-transition"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}