tui-cs/Terminal.Gui · error · InvalidOperationException

{schemeName}: Cannot remove a built-in Scheme.

Error message

{schemeName}: Cannot remove a built-in Scheme.

What it means

RemoveScheme rejects names that map to a built-in Schemes enum value (SchemeNameToSchemes returns non-null). Built-in schemes (e.g. TopLevel, Dialog, Menu, Error) are protected and cannot be removed — only modified.

Source

Thrown at Terminal.Gui/Configuration/SchemeManager.cs:100

    /// <returns></returns>
    public static void AddScheme (string schemeName, Scheme scheme)
    {
        if (!GetSchemes ().TryAdd (schemeName, scheme))
        {
            GetSchemes () [schemeName] = scheme;
        }
    }

    /// <summary>
    ///     Removes a Scheme from <see cref="SchemeManager"/>.
    /// </summary>
    /// <param name="schemeName"></param>
    /// <exception cref="InvalidOperationException">If the scheme is a built-in Scheme or was not previously added.</exception>
    public static void RemoveScheme (string schemeName)
    {
        if (SchemeNameToSchemes (schemeName) is { })
        {
            throw new InvalidOperationException ($@"{schemeName}: Cannot remove a built-in Scheme.");
        }

        if (!GetSchemes ().TryGetValue (schemeName, out _))
        {
            throw new InvalidOperationException ($@"{schemeName}: Does not exist in Schemes.");
        }

        GetSchemes ().Remove (schemeName);
    }

    /// <summary>
    ///     Gets the <see cref="Scheme"/> for the specified <see cref="Drawing.Schemes"/>.
    /// </summary>
    /// <param name="schemeName"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    public static Scheme GetScheme (Schemes schemeName)
    {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Do not remove built-in schemes; modify them in place via AddScheme (which updates an existing name).
  2. Filter out built-in names (SchemeNameToSchemes(name) != null) before calling RemoveScheme.

Example fix

// before
SchemeManager.RemoveScheme ("Dialog");
// after
// built-ins cannot be removed; update in place instead
SchemeManager.AddScheme ("Dialog", myScheme);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsRemovable (string name) =>
    SchemeManager.SchemeNameToSchemes (name) is null;

Try / catch

try { SchemeManager.RemoveScheme (name); }
catch (InvalidOperationException ex) when (ex.Message.Contains ("Cannot remove a built-in"))
{ /* name is a built-in; modify via AddScheme instead */ }

Prevention

When it happens

Trigger: Calling SchemeManager.RemoveScheme("TopLevel") or any other built-in scheme name.

Common situations: App code that dynamically manages schemes and tries to clean up a built-in; generic "remove all schemes" cleanup logic.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/fb0d8c76b17ad7c7. Report an issue: GitHub.