Skip to content

--select-model cannot plan a model deletion without an active model in the same selection #5741

Closed

Description

@gjesse

Bug: --select-model cannot plan a model deletion without an active model in the same selection

Describe the bug

When a model file is deleted from the project and you attempt to plan its removal using
--select-model=<model-name>, SQLMesh raises:

Error: Selector did not return any models. Please check your model selection and try again.

This happens regardless of whether the deletion is unstaged, staged, or committed.

Root cause

In selector.py, get_model() returns None for any model matched via env_models (the
deployed environment state) that is absent from self._models (current project files). Because
the file is deleted, it cannot be in self._models. The model is excluded from
models_override, so backfill_models ends up empty. context._plan() then raises PlanError
when it detects backfill_models is not None and not backfill_models.

Workaround

The deletion appears in the plan if at least one other active (non-deleted) model is also
selected. In that case backfill_models is non-empty, the deleted model is excluded from
models_override, and ContextDiff naturally surfaces it in removed_snapshots.

sqlmesh plan --select-model=<deleted-model> --select-model=<any-active-model>

This is fragile — it requires the user to know about the constraint and artificially include an
unrelated model.

▎ Note: The git:master selector uses --diff-filter=d on all its git diff calls, which
▎ explicitly excludes deleted files. There is currently no selector-based path that can surface a
▎ model deletion without an active model alongside it.

Expected behavior

--select-model= should be sufficient to plan a model removal when that
model exists in the deployed environment state, without requiring an unrelated active model in
the selection.

Steps to reproduce

  1. Deploy a model to an environment
  2. Delete the model's .sql file from the project
  3. Run sqlmesh plan --select-model=
  4. Observe: PlanError: Selector did not return any models
  5. Add any other active model: sqlmesh plan --select-model= --select-model=
  6. Observe: deletion appears in the plan correctly

Environment

  - SQLMesh Core: 0.230.1
  - Tobiko Cloud Enterprise: 202608.1.0+534bbc3

Activity

  1. added 2 commits that reference this issue on Apr 5, 2026
    93e5928
    da9a69c
  2. viniciusnunest commented on Apr 5, 2026

    @viniciusnunest
    Contributor

    Hi, I looked into this and got a fix working.

    Your root cause analysis is spot on by the way. It boils down to expand_model_selections only searching local models, so deleted ones just vanish from the selection. Then _plan() raises because backfill_models ends up empty.

    What I did:

    • Pulled the environment loading logic from select_models() into a _load_env_models() helper, there was some duplication there already
    • Added expand_model_selections_with_env() that searches both local and environment models
    • In _plan(), when backfill_models is empty, it now checks if the selection matched something in the deployed environment that got deleted locally. If yes, PlanError is skipped and the deletion shows up through context_diff.removed_snapshots as it should
    • Selecting a model that does not exist anywhere still raises PlanError, no change there

    Wrote 3 unit tests for the new selector method (env lookup, fallback, expired env) and 2 integration tests with the sushi project, one happy path and one negative case.

    PR: #5759

  3. added a commit that references this issue on Apr 11, 2026
    feafd50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions