Skip to content

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

@gjesse

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

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