{"record":{"id":"f6a44f1fa4e0c491","repo":"OrchardCMS/OrchardCore","slug":"unable-to-remove-system-defined-type","errorCode":null,"errorMessage":"Unable to remove system-defined type.","messagePattern":"Unable to remove system-defined type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs","lineNumber":118,"sourceCode":"\n        return contentTypeDefinition;\n    }\n\n    public async Task RemoveTypeAsync(string name, bool deleteContent)\n    {\n        // First remove all attached parts.\n        var typeDefinition = await _contentDefinitionManager.LoadTypeDefinitionAsync(name);\n\n        if (typeDefinition == null)\n        {\n            return;\n        }\n\n        var settings = typeDefinition.GetSettings<ContentSettings>();\n\n        if (settings.IsSystemDefined)\n        {\n            throw new InvalidOperationException(\"Unable to remove system-defined type.\");\n        }\n\n        var partDefinitions = typeDefinition.Parts.ToList();\n        foreach (var partDefinition in partDefinitions)\n        {\n            await RemovePartFromTypeAsync(partDefinition.PartDefinition.Name, name);\n\n            // Delete the part if it's its own part.\n            if (partDefinition.PartDefinition.Name == name)\n            {\n                await RemovePartAsync(name);\n            }\n        }\n\n        await _contentDefinitionManager.DeleteTypeDefinitionAsync(name);\n\n        var context = new ContentTypeRemovedContext\n        {","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs#L100-L136","documentation":"RemoveTypeAsync checks the target type's ContentSettings and throws InvalidOperationException if IsSystemDefined is true. System-defined types (declared by modules/features) are considered infrastructure and must not be deleted through the content definition API.","triggerScenarios":"Calling IContentDefinitionService.RemoveTypeAsync(name) on a type whose settings have IsSystemDefined=true, e.g. built-in types like User or types marked system-defined via ContentSettings.","commonSituations":"Trying to 'clean up' built-in content types via a migration, recipe step, or admin-less code path; deleting all types in a loop without skipping system ones.","solutions":["Check typeDefinition.GetSettings<ContentSettings>().IsSystemDefined before calling RemoveTypeAsync and skip such types.","If the type truly must not exist, disable the feature/module that declares it instead of deleting its definition.","Remove the IsSystemDefined flag only if you own the definition and understand the consequences (via an editor or migration), then remove it."],"exampleFix":"// before\nawait contentDefinitionService.RemoveTypeAsync(\"User\");\n// after\nvar def = await contentDefinitionManager.LoadTypeDefinitionAsync(\"User\");\nif (def?.GetSettings<ContentSettings>().IsSystemDefined != true)\n{\n    await contentDefinitionService.RemoveTypeAsync(\"User\");\n}","handlingStrategy":"validation","validationCode":"// C#\nvar def = await contentDefinitionManager.LoadTypeDefinitionAsync(typeName);\nbool removable = def is not null && !def.GetSettings<ContentSettings>().IsSystemDefined;","typeGuard":"static bool IsRemovable(TypeDefinition def) =>\n    def is not null && !def.GetSettings<ContentSettings>().IsSystemDefined;","tryCatchPattern":"try\n{\n    await contentDefinitionService.RemoveTypeAsync(typeName);\n}\ncatch (InvalidOperationException)\n{\n    // skip or surface: type is system-defined\n}","preventionTips":["Check IsSystemDefined before any bulk delete of types.","Never delete types owned by enabled modules; disable the feature instead.","Track which types your own module created and only remove those."],"tags":["contenttypes","system-defined","invalid-state-transition"],"backgroundTag":"unsupported-operation","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}