You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
--select-model cannot plan a model deletion without an active model in the same selection #5741
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
Deploy a model to an environment
Delete the model's .sql file from the project
Run sqlmesh plan --select-model=
Observe: PlanError: Selector did not return any models
Add any other active model: sqlmesh plan --select-model= --select-model=
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.
Activity
Fix: allow --select-model to plan a model deletion
Fix: allow --select-model to plan a model deletion
viniciusnunest commentedon Apr 5, 2026
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_selectionsonly searching local models, so deleted ones just vanish from the selection. Then_plan()raises becausebackfill_modelsends up empty.What I did:
select_models()into a_load_env_models()helper, there was some duplication there alreadyexpand_model_selections_with_env()that searches both local and environment models_plan(), whenbackfill_modelsis empty, it now checks if the selection matched something in the deployed environment that got deleted locally. If yes,PlanErroris skipped and the deletion shows up throughcontext_diff.removed_snapshotsas it shouldPlanError, no change thereWrote 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
Fix: allow --select-model to plan a model deletion