Skip to main content

How to Stop a Task from Pushing an XCom in Apache Airflow® with do_xcom_push=False

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

By default, most Apache Airflow® tasks store their result as an XCom, so that another task can use it. A BashOperator stores the last line its command prints, even when no other task needs it. do_xcom_push=False turns that off for one task. This guide compares two BashOperator tasks on Airflow 3.4.0, one with the default and one with do_xcom_push=False.

Syntax​

Syntax
<Operator>(
task_id="<task id>",
<other parameters>,
do_xcom_push=False,
)
PartWhat it means
<Operator>Any operator. Every operator has do_xcom_push, because it's a parameter of their common base class, BaseOperator
do_xcom_push=FalseDon't store the task's result as an XCom. The default, True, stores it under the key return_value

A @task function takes it the same way: @task(do_xcom_push=False).

The BaseOperator reference describes the parameter as "if True, an XCom is pushed containing the Operator’s result". For BashOperator, the result is the last line of output: the BashOperator reference says that with do_xcom_push on, "the last line written to stdout will also be pushed to an XCom when the bash command completes".

The Example​

A Dag called bash_xcom has two BashOperator tasks that both run echo "first line"; echo "last line". push_last_line keeps the default, and push_nothing sets do_xcom_push=False.

Prerequisites​

  • Apache Airflow® running, for example with airflow standalone, and its dags folder in your AIRFLOW_HOME.

Step 1: Write the Dag​

Create dags/bash_xcom.py:

dags/bash_xcom.py
from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import dag


@dag(schedule=None, tags=["demo"])
def bash_xcom():
BashOperator(
task_id="push_last_line",
bash_command='echo "first line"; echo "last line"',
)
BashOperator(
task_id="push_nothing",
bash_command='echo "first line"; echo "last line"',
do_xcom_push=False,
)


bash_xcom()

The bash_xcom.py Dag file in an editor window: two BashOperator tasks running echo "first line"; echo "last line", the second one, push_nothing, with do_xcom_push=False.

Both tasks print two lines. The only difference is do_xcom_push=False on push_nothing.

Step 2: See What the Default Stores​

Trigger the Dag, then open the push_last_line task and its XCom tab (How to See a Task's XComs in the Apache Airflow® UI shows where to click):

The push_last_line task instance in the bash_xcom Dag, XCom tab: 1 XCom Entry, with the key return_value and the value last line.

One entry, return_value, with the value last line. The command printed two lines, and only the last one was stored.

Step 3: See That the Other Task Stores Nothing​

Click the run's square for push_nothing:

The push_nothing task instance in the same run, XCom tab: 0 XCom Entries and the message No XCom Entries found.

0 XCom Entries and No XCom Entries found: the task ran the same command and succeeded, and stored nothing. In this example's metadata database, the database where Airflow keeps its records, push_last_line had a row in the XCom table and push_nothing had none.

Common Mistakes​

MistakeWhat happensDo this instead
Turning it off on a task whose result another task usesNothing fails: the next task gets None. In a check, tasks that read the results of a @task(do_xcom_push=False) function and of a BashOperator with do_xcom_push=False both got NoneKeep the default on tasks whose result is passed on
Expecting every line of a BashOperator's output in the XComOnly the last line is stored: last line, not first linePrint the value to pass on last

Troubleshooting​

SymptomLikely causeFix
A task gets None from the task before itThe earlier task has do_xcom_push=FalseRemove do_xcom_push=False from it
No XCom Entries found in the XCom tabThe task has do_xcom_push=False, or it returned nothingExpected if no other task needs its result

FAQ​

When should I turn it off?​

When no other task uses the result. XComs "are only designed for small amounts of data", says the XCom documentation, and a BashOperator stores its last line whatever it is. With do_xcom_push=False, nothing is stored.

Does it work with @task?​

Yes. @task(do_xcom_push=False) on a function that returned 5 stored no XCom.

Conclusion​

With the default, push_last_line stored its command's last line, last line, under return_value. With do_xcom_push=False, push_nothing ran the same command and stored nothing. Turn it off only on tasks whose result no other task needs, because a task that reads it gets None.

Additional Resources​

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.