Skip to content

Add partition clear support to REST API matching the CLI#68702

Merged
vatsrahul1001 merged 14 commits into
apache:mainfrom
astronomer:partition-clear-rest-api
Jun 23, 2026
Merged

Add partition clear support to REST API matching the CLI#68702
vatsrahul1001 merged 14 commits into
apache:mainfrom
astronomer:partition-clear-rest-api

Conversation

@Lee-W

@Lee-W Lee-W commented Jun 18, 2026

Copy link
Copy Markdown
Member

Brings the airflow dags clear / airflow partitions clear partition-clear capability to the REST API, consolidates the now-shared core between CLI and REST so they can't drift, and fixes a sub-day window bug found along the way.

Why

  • REST had no way to clear by partition selector — only the CLI could, so any partition-aware client had to shell out.
  • The date-window filter and the column-reset / task-instance batching logic existed in two parallel copies (REST service + CLI); divergence was a matter of time.
  • A run_id shared across Dags let a partition clear touch another Dag's task instances.
  • Bulk clear by partition selector skipped the per-Dag authorization check, so a user without clear access to the target Dag could still reset its runs.
  • dags clear / partitions clear widened any sub-day partition bound to the whole local day — on an hourly Dag, --partition-date-start 08:00 --partition-date-end 08:00 cleared all 24 partitions instead of one.

What

  • clearDagRuns accepts partition_key / partition_date window selectors as an alternative to an explicit run list.
  • New POST /dags/{dag_id}/clearPartitions resets partition_key / partition_date on matching runs, with optional task-instance clear.
  • DagRun.clear_partition_runs (column reset + TI batching + dry-run count) and DagRun.apply_partition_date_window (window filter) now back both REST and CLI.
  • PartitionSelectorMixin dedups the partition selector fields + date-order check across BulkDAGRunClearBody and ClearPartitionsBody.
  • Partition-clear TI lookups are now scoped by dag_id.
  • Bulk clear by partition selector now adds the target Dag's PUT authorization request, so it is gated by the same access check as an explicit run list.
  • Timetable.localize_partition_datetime (base: UTC pass-through; CronMixin: wall-clock in the timetable's timezone) replaces the day-bound widening; window end is now an inclusive <= instead of half-open < next_midnight. It also subsumes the partition backfill iteration's private localization helper, so backfill and clear now share one wall-clock conversion.

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [Claude] following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@boring-cyborg boring-cyborg Bot added the area:API Airflow's REST/HTTP API label Jun 18, 2026
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch 2 times, most recently from d5b88c4 to a49ae18 Compare June 18, 2026 13:11
@Lee-W Lee-W changed the title API: Add partition clear support to REST API to match the CLI Add partition clear support to REST API to match the CLI Jun 18, 2026
@Lee-W Lee-W added the backport-to-v3-3-test Backport to v3-3-test label Jun 18, 2026
@Lee-W Lee-W marked this pull request as ready for review June 18, 2026 13:36
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch 3 times, most recently from aa09898 to 292ce16 Compare June 19, 2026 01:12

@pierrejeambrun pierrejeambrun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions / fix.

Also the date-window semantics here don't actually match airflow partitions clear, which the PR aims to mirror. The CLI compares raw values (partition_date >= start / <= end, partition_command.py:100-103), while this resolves calendar-day bounds (>= resolve_day_bound(start) / < resolve_day_bound(end+1day)). For the same inputs the two clear different runs at the end boundary (and across timezones). Your calendar-day version actually matches the documented "inclusive, calendar-day granular" intent better than the CLI's <= end does — so I think the right fix is to make the CLI use the same resolve_day_bound logic (or share a helper), rather than leave the two interfaces inconsistent. Worth confirming the intended boundary behavior either way.

Comment thread airflow-core/src/airflow/api_fastapi/core_api/datamodels/dag_run.py
Comment thread airflow-core/src/airflow/api_fastapi/core_api/security.py Outdated
Comment thread airflow-core/src/airflow/api_fastapi/core_api/services/public/dag_run.py Outdated
Comment thread airflow-core/src/airflow/api_fastapi/core_api/services/public/dag_run.py Outdated
Comment thread airflow-core/src/airflow/api_fastapi/core_api/services/public/dag_run.py Outdated
Comment thread airflow-core/src/airflow/api_fastapi/core_api/security.py Outdated
@Lee-W Lee-W added this to the Airflow 3.3.0 milestone Jun 19, 2026
@Lee-W Lee-W self-assigned this Jun 22, 2026
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch from 93ad895 to 0f3a84b Compare June 22, 2026 09:08
@Lee-W Lee-W requested review from XD-DENG and ashb as code owners June 22, 2026 09:08
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch 2 times, most recently from 17da6bf to 104ee6d Compare June 22, 2026 15:54
@Lee-W Lee-W changed the title Add partition clear support to REST API to match the CLI Add partition clear support to REST API matching the CLI Jun 23, 2026
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch from 5a0e85e to 4ba184e Compare June 23, 2026 08:41
Comment thread airflow-core/src/airflow/api_fastapi/core_api/datamodels/dag_run.py Outdated
Comment thread airflow-core/src/airflow/api_fastapi/core_api/datamodels/dag_run.py Outdated
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch from c34c93a to d9fe531 Compare June 23, 2026 09:57
@Lee-W Lee-W requested a review from uranusjr June 23, 2026 10:13
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch from 723456d to bcca769 Compare June 23, 2026 10:27
Lee-W added 13 commits June 23, 2026 19:10
clearDagRuns now accepts partition_key / partition_date window selectors
as an alternative to an explicit run list. Add POST /dags/{dag_id}/clearPartitions
to reset partition_key/partition_date on matching runs, with optional
task-instance clear — REST parity with `airflow dags clear` / `airflow partitions clear`.
Extract the shared partition_key / partition_date window fields and their date-order check into a PartitionSelectorMixin reused by BulkDAGRunClearBody and ClearPartitionsBody, and replace the repeated partition-selector presence checks with a has_partition_selectors property. No behavior change.
Add a dag_id filter to the task-instance lookups in both the REST clear_partition_fields service and the airflow partitions clear CLI so a run_id shared across dags no longer clears another dag's task instances, and collapse the per-run dry-run task-instance lookups into a single batched count query.
Extract the resolve_day_bound partition_date window resolution duplicated across the REST clear_dag_runs route, the clear_partition_fields service, and the airflow partitions clear CLI into a single DagRun.apply_partition_date_window helper so the three cannot drift.
Extract the partition column-reset, task-instance batching, and dry-run counting into a single DagRun.clear_partition_runs helper reused by the clearPartitions REST endpoint and the airflow partitions clear CLI, replacing the two parallel implementations. The CLI keeps its per-run output through an optional callback. No behavior change.
`dags clear` and `partitions clear` passed user-supplied datetimes
through `resolve_day_bound(.date())`, which stripped the time component
and expanded any sub-day bound to local midnight. On an hourly
partitioned Dag, `--partition-date-start 08:00 --partition-date-end
08:00` cleared all 24 partitions instead of just the 08:00 one.

Adds `localize_partition_datetime` to the `Timetable` protocol (base:
UTC pass-through; CronMixin: wall-clock re-interpreted in the
timetable's local timezone, same logic as apache#68718). Removes the
now-redundant private `_localize_wall_clock_to_timetable_timezone` from
`CronPartitionTimetable`. Updates `apply_partition_date_window` to use
the new method with an inclusive `<=` end bound instead of the old
half-open `< next_midnight` form.
BulkDAGRunClearBody and ClearPartitionsBody duplicated the same
"exactly one selection mode" rule, including the partition-window
definition and the selector-enumeration error message, which would
drift independently. Move the shared check onto PartitionSelectorMixin
so the partition-selector semantics live in one place.
The "sub-day precision is preserved" phrasing framed the help against a
since-fixed truncation bug, which is meaningless to a reader seeing the
text fresh. The timezone re-interpretation note plus the date-only ->
midnight rule already convey that the time of day is honoured.
@Lee-W Lee-W force-pushed the partition-clear-rest-api branch from bcca769 to b482bd0 Compare June 23, 2026 11:10

@pierrejeambrun pierrejeambrun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

CI isn't happy

A loop variable shadowed the imported `task` decorator (ruff F402) and a
British-spelled word in a new docstring tripped the en_US docs spell-check.
@vatsrahul1001 vatsrahul1001 merged commit a0805a8 into apache:main Jun 23, 2026
281 of 283 checks passed
@vatsrahul1001 vatsrahul1001 deleted the partition-clear-rest-api branch June 23, 2026 15:44
@github-actions

Copy link
Copy Markdown
Contributor

Backport successfully created: v3-3-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
v3-3-test PR Link

github-actions Bot pushed a commit to aws-mwaa/upstream-to-airflow that referenced this pull request Jun 23, 2026
…pache#68702)

* API: Add partition clear support to REST API to match the CLI

clearDagRuns now accepts partition_key / partition_date window selectors
as an alternative to an explicit run list. Add POST /dags/{dag_id}/clearPartitions
to reset partition_key/partition_date on matching runs, with optional
task-instance clear — REST parity with `airflow dags clear` / `airflow partitions clear`.

* API: Deduplicate partition selector fields across clear request bodies

Extract the shared partition_key / partition_date window fields and their date-order check into a PartitionSelectorMixin reused by BulkDAGRunClearBody and ClearPartitionsBody, and replace the repeated partition-selector presence checks with a has_partition_selectors property. No behavior change.

* Scope partition-clear task instance queries to the target dag

Add a dag_id filter to the task-instance lookups in both the REST clear_partition_fields service and the airflow partitions clear CLI so a run_id shared across dags no longer clears another dag's task instances, and collapse the per-run dry-run task-instance lookups into a single batched count query.

* Share the partition date-window filter across clear paths

Extract the resolve_day_bound partition_date window resolution duplicated across the REST clear_dag_runs route, the clear_partition_fields service, and the airflow partitions clear CLI into a single DagRun.apply_partition_date_window helper so the three cannot drift.

* Share the partition-clear core between the REST API and the CLI

Extract the partition column-reset, task-instance batching, and dry-run counting into a single DagRun.clear_partition_runs helper reused by the clearPartitions REST endpoint and the airflow partitions clear CLI, replacing the two parallel implementations. The CLI keeps its per-run output through an optional callback. No behavior change.

* Fix partition clear commands widening sub-day windows to the whole day

`dags clear` and `partitions clear` passed user-supplied datetimes
through `resolve_day_bound(.date())`, which stripped the time component
and expanded any sub-day bound to local midnight. On an hourly
partitioned Dag, `--partition-date-start 08:00 --partition-date-end
08:00` cleared all 24 partitions instead of just the 08:00 one.

Adds `localize_partition_datetime` to the `Timetable` protocol (base:
UTC pass-through; CronMixin: wall-clock re-interpreted in the
timetable's local timezone, same logic as apache#68718). Removes the
now-redundant private `_localize_wall_clock_to_timetable_timezone` from
`CronPartitionTimetable`. Updates `apply_partition_date_window` to use
the new method with an inclusive `<=` end bound instead of the old
half-open `< next_midnight` form.

* Update REST datamodel descriptions to reflect sub-day precision

* Share partition selection-mode validation across clear request bodies

BulkDAGRunClearBody and ClearPartitionsBody duplicated the same
"exactly one selection mode" rule, including the partition-window
definition and the selector-enumeration error message, which would
drift independently. Move the shared check onto PartitionSelectorMixin
so the partition-selector semantics live in one place.

* Drop sub-day-precision wording from partition clear CLI help

The "sub-day precision is preserved" phrasing framed the help against a
since-fixed truncation bug, which is meaningless to a reader seeing the
text fresh. The timezone re-interpretation note plus the date-only ->
midnight rule already convey that the time of day is honoured.

* Remove unused resolve_day_bound

* Refactor tests

* regen docs

* Fix test failure

* Fix ruff F402 and docs spelling failing CI on partition-clear branch

A loop variable shadowed the imported `task` decorator (ruff F402) and a
British-spelled word in a new docstring tripped the en_US docs spell-check.

---------
(cherry picked from commit a0805a8)

Co-authored-by: Wei Lee <weilee.rx@gmail.com>
Co-authored-by: Rahul Vats <rah.sharma11@gmail.com>
aws-airflow-bot pushed a commit to aws-mwaa/upstream-to-airflow that referenced this pull request Jun 23, 2026
…pache#68702)

* API: Add partition clear support to REST API to match the CLI

clearDagRuns now accepts partition_key / partition_date window selectors
as an alternative to an explicit run list. Add POST /dags/{dag_id}/clearPartitions
to reset partition_key/partition_date on matching runs, with optional
task-instance clear — REST parity with `airflow dags clear` / `airflow partitions clear`.

* API: Deduplicate partition selector fields across clear request bodies

Extract the shared partition_key / partition_date window fields and their date-order check into a PartitionSelectorMixin reused by BulkDAGRunClearBody and ClearPartitionsBody, and replace the repeated partition-selector presence checks with a has_partition_selectors property. No behavior change.

* Scope partition-clear task instance queries to the target dag

Add a dag_id filter to the task-instance lookups in both the REST clear_partition_fields service and the airflow partitions clear CLI so a run_id shared across dags no longer clears another dag's task instances, and collapse the per-run dry-run task-instance lookups into a single batched count query.

* Share the partition date-window filter across clear paths

Extract the resolve_day_bound partition_date window resolution duplicated across the REST clear_dag_runs route, the clear_partition_fields service, and the airflow partitions clear CLI into a single DagRun.apply_partition_date_window helper so the three cannot drift.

* Share the partition-clear core between the REST API and the CLI

Extract the partition column-reset, task-instance batching, and dry-run counting into a single DagRun.clear_partition_runs helper reused by the clearPartitions REST endpoint and the airflow partitions clear CLI, replacing the two parallel implementations. The CLI keeps its per-run output through an optional callback. No behavior change.

* Fix partition clear commands widening sub-day windows to the whole day

`dags clear` and `partitions clear` passed user-supplied datetimes
through `resolve_day_bound(.date())`, which stripped the time component
and expanded any sub-day bound to local midnight. On an hourly
partitioned Dag, `--partition-date-start 08:00 --partition-date-end
08:00` cleared all 24 partitions instead of just the 08:00 one.

Adds `localize_partition_datetime` to the `Timetable` protocol (base:
UTC pass-through; CronMixin: wall-clock re-interpreted in the
timetable's local timezone, same logic as apache#68718). Removes the
now-redundant private `_localize_wall_clock_to_timetable_timezone` from
`CronPartitionTimetable`. Updates `apply_partition_date_window` to use
the new method with an inclusive `<=` end bound instead of the old
half-open `< next_midnight` form.

* Update REST datamodel descriptions to reflect sub-day precision

* Share partition selection-mode validation across clear request bodies

BulkDAGRunClearBody and ClearPartitionsBody duplicated the same
"exactly one selection mode" rule, including the partition-window
definition and the selector-enumeration error message, which would
drift independently. Move the shared check onto PartitionSelectorMixin
so the partition-selector semantics live in one place.

* Drop sub-day-precision wording from partition clear CLI help

The "sub-day precision is preserved" phrasing framed the help against a
since-fixed truncation bug, which is meaningless to a reader seeing the
text fresh. The timezone re-interpretation note plus the date-only ->
midnight rule already convey that the time of day is honoured.

* Remove unused resolve_day_bound

* Refactor tests

* regen docs

* Fix test failure

* Fix ruff F402 and docs spelling failing CI on partition-clear branch

A loop variable shadowed the imported `task` decorator (ruff F402) and a
British-spelled word in a new docstring tripped the en_US docs spell-check.

---------
(cherry picked from commit a0805a8)

Co-authored-by: Wei Lee <weilee.rx@gmail.com>
Co-authored-by: Rahul Vats <rah.sharma11@gmail.com>
Lee-W added a commit that referenced this pull request Jun 24, 2026
…68702) (#68907)

Co-authored-by: Wei Lee <weilee.rx@gmail.com>
Co-authored-by: Rahul Vats <rah.sharma11@gmail.com>
cetingokhan pushed a commit to cetingokhan/airflow that referenced this pull request Jun 24, 2026
* API: Add partition clear support to REST API to match the CLI

clearDagRuns now accepts partition_key / partition_date window selectors
as an alternative to an explicit run list. Add POST /dags/{dag_id}/clearPartitions
to reset partition_key/partition_date on matching runs, with optional
task-instance clear — REST parity with `airflow dags clear` / `airflow partitions clear`.

* API: Deduplicate partition selector fields across clear request bodies

Extract the shared partition_key / partition_date window fields and their date-order check into a PartitionSelectorMixin reused by BulkDAGRunClearBody and ClearPartitionsBody, and replace the repeated partition-selector presence checks with a has_partition_selectors property. No behavior change.

* Scope partition-clear task instance queries to the target dag

Add a dag_id filter to the task-instance lookups in both the REST clear_partition_fields service and the airflow partitions clear CLI so a run_id shared across dags no longer clears another dag's task instances, and collapse the per-run dry-run task-instance lookups into a single batched count query.

* Share the partition date-window filter across clear paths

Extract the resolve_day_bound partition_date window resolution duplicated across the REST clear_dag_runs route, the clear_partition_fields service, and the airflow partitions clear CLI into a single DagRun.apply_partition_date_window helper so the three cannot drift.

* Share the partition-clear core between the REST API and the CLI

Extract the partition column-reset, task-instance batching, and dry-run counting into a single DagRun.clear_partition_runs helper reused by the clearPartitions REST endpoint and the airflow partitions clear CLI, replacing the two parallel implementations. The CLI keeps its per-run output through an optional callback. No behavior change.

* Fix partition clear commands widening sub-day windows to the whole day

`dags clear` and `partitions clear` passed user-supplied datetimes
through `resolve_day_bound(.date())`, which stripped the time component
and expanded any sub-day bound to local midnight. On an hourly
partitioned Dag, `--partition-date-start 08:00 --partition-date-end
08:00` cleared all 24 partitions instead of just the 08:00 one.

Adds `localize_partition_datetime` to the `Timetable` protocol (base:
UTC pass-through; CronMixin: wall-clock re-interpreted in the
timetable's local timezone, same logic as apache#68718). Removes the
now-redundant private `_localize_wall_clock_to_timetable_timezone` from
`CronPartitionTimetable`. Updates `apply_partition_date_window` to use
the new method with an inclusive `<=` end bound instead of the old
half-open `< next_midnight` form.

* Update REST datamodel descriptions to reflect sub-day precision

* Share partition selection-mode validation across clear request bodies

BulkDAGRunClearBody and ClearPartitionsBody duplicated the same
"exactly one selection mode" rule, including the partition-window
definition and the selector-enumeration error message, which would
drift independently. Move the shared check onto PartitionSelectorMixin
so the partition-selector semantics live in one place.

* Drop sub-day-precision wording from partition clear CLI help

The "sub-day precision is preserved" phrasing framed the help against a
since-fixed truncation bug, which is meaningless to a reader seeing the
text fresh. The timezone re-interpretation note plus the date-only ->
midnight rule already convey that the time of day is honoured.

* Remove unused resolve_day_bound

* Refactor tests

* regen docs

* Fix test failure

* Fix ruff F402 and docs spelling failing CI on partition-clear branch

A loop variable shadowed the imported `task` decorator (ruff F402) and a
British-spelled word in a new docstring tripped the en_US docs spell-check.

---------

Co-authored-by: Rahul Vats <rah.sharma11@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API backport-to-v3-3-test Backport to v3-3-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants