<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to stop Databricks retaining widget selection between runs? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-stop-databricks-retaining-widget-selection-between-runs/m-p/141659#M51781</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I have a Python notebook in Databricks. Within it I have a multiselect widget, which is defined like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;widget_values = spark.sql(f'''
SELECT my_column
FROM my_table
GROUP BY my_column
ORDER BY my_column
''')

widget_values = widget_values.collect()
widget_values = [i[0] for i in widget_values]

if len(widget_values) &amp;gt;0:
dbutils.widgets.multiselect("My widget", widget_values[0], widget_values)
selection = dbutils.widgets.get("My widget")
else:
print("No data in my_table")&lt;/LI-CODE&gt;&lt;P&gt;I want the variable 'selection' to be blank if no values are selected in the widget. However, once a value or values has been selected, it gets retained until another selection overwrites it.&lt;/P&gt;&lt;P&gt;For example, say I select the value 'A' in the widget and run the notebook. Selection now = 'A'. I then untick 'A' in the widget and run the notebook again. Selection should = '', but it still = 'A'. This happens even if I delete the value 'A' from the table between the two runs.&lt;/P&gt;&lt;P&gt;I have tried adding this code to the line before selection is chosen, but it doesn't do anything:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selection = ''&lt;/LI-CODE&gt;&lt;P&gt;I have also tried starting a new session on the compute I'm using to run the notebook. If I do this, no variables appear in the 'variables' pane until I run the notebook again. But if I then run the notebook with no values selected in the widget, suddenly selection = 'A' again.&lt;/P&gt;&lt;P&gt;Does anyone know why Databricks is retaining the selection like this and how I can stop it please?&lt;/P&gt;</description>
    <pubDate>Thu, 11 Dec 2025 12:07:03 GMT</pubDate>
    <dc:creator>SRJDB</dc:creator>
    <dc:date>2025-12-11T12:07:03Z</dc:date>
    <item>
      <title>How to stop Databricks retaining widget selection between runs?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-stop-databricks-retaining-widget-selection-between-runs/m-p/141659#M51781</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a Python notebook in Databricks. Within it I have a multiselect widget, which is defined like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;widget_values = spark.sql(f'''
SELECT my_column
FROM my_table
GROUP BY my_column
ORDER BY my_column
''')

widget_values = widget_values.collect()
widget_values = [i[0] for i in widget_values]

if len(widget_values) &amp;gt;0:
dbutils.widgets.multiselect("My widget", widget_values[0], widget_values)
selection = dbutils.widgets.get("My widget")
else:
print("No data in my_table")&lt;/LI-CODE&gt;&lt;P&gt;I want the variable 'selection' to be blank if no values are selected in the widget. However, once a value or values has been selected, it gets retained until another selection overwrites it.&lt;/P&gt;&lt;P&gt;For example, say I select the value 'A' in the widget and run the notebook. Selection now = 'A'. I then untick 'A' in the widget and run the notebook again. Selection should = '', but it still = 'A'. This happens even if I delete the value 'A' from the table between the two runs.&lt;/P&gt;&lt;P&gt;I have tried adding this code to the line before selection is chosen, but it doesn't do anything:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selection = ''&lt;/LI-CODE&gt;&lt;P&gt;I have also tried starting a new session on the compute I'm using to run the notebook. If I do this, no variables appear in the 'variables' pane until I run the notebook again. But if I then run the notebook with no values selected in the widget, suddenly selection = 'A' again.&lt;/P&gt;&lt;P&gt;Does anyone know why Databricks is retaining the selection like this and how I can stop it please?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Dec 2025 12:07:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-stop-databricks-retaining-widget-selection-between-runs/m-p/141659#M51781</guid>
      <dc:creator>SRJDB</dc:creator>
      <dc:date>2025-12-11T12:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to stop Databricks retaining widget selection between runs?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-stop-databricks-retaining-widget-selection-between-runs/m-p/141663#M51783</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/199244"&gt;@SRJDB&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P class="p1"&gt;What you’re running into isn’t your Python variable misbehaving—it’s the widget hanging onto its own internal state. A Databricks widget will happily keep whatever value you gave it, per user and per notebook, until you explicitly clear or overwrite it. So when you do &lt;SPAN class="s2"&gt;selection = ''&lt;/SPAN&gt; in Python, that doesn’t touch the widget’s bound value returned by &lt;SPAN class="s2"&gt;dbutils.widgets.get()&lt;/SPAN&gt;. To make things more fun, widgets also have a long-standing quirk where state doesn’t always clear after a Run All, which explains why the panel and the printed value sometimes disagree.&lt;/P&gt;
&lt;P class="p1"&gt;Let’s dig into why this happens.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;dbutils.widgets.get()&lt;/SPAN&gt; always returns the widget’s stored value, not whatever Python variable you decide should be “the truth.”&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Multiselect widgets send back a comma-delimited string. If nothing is selected it should be empty—unless the widget resurrects a previous state or you recreate it with a baked-in default.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;And that default matters. With &lt;SPAN class="s1"&gt;defaultValue=widget_values[0]&lt;/SPAN&gt;, you’ve guaranteed that every redefinition forces a selection. There’s no way to end up blank unless you explicitly allow one.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Bonus: after a Run All, widgets sometimes refuse to clear state, even if you remove/reset them in code. Running the cells individually usually sidesteps that. If you ever need fully consistent state behavior, ipywidgets are the more predictable path.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="p1"&gt;Now here are the patterns that actually work.&lt;/P&gt;
&lt;P class="p1"&gt;Option 1: Remove and recreate the widget before reading it.&lt;/P&gt;
&lt;P class="p1"&gt;And yes—removal must happen in a different cell.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;# Cell 1: Reset widget
try:
    dbutils.widgets.remove("My widget")
except Exception:
    pass&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE&gt;# Cell 2: Recreate and read
widget_values = spark.sql("""
  SELECT my_column
  FROM my_table
  GROUP BY my_column
  ORDER BY my_column
""").collect()
widget_values = [r[0] for r in widget_values]

choices = [""] + widget_values  # Explicit blank
dbutils.widgets.multiselect("My widget", "", choices)

raw = dbutils.widgets.get("My widget")
selection_list = [s for s in raw.split(",") if s.strip()]
selection = "" if not selection_list else ",".join(selection_list)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;Option 2: Add a labeled sentinel (“”) and filter it out cleanly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;choices = ["&amp;lt;none&amp;gt;"] + widget_values
dbutils.widgets.multiselect("My widget", "&amp;lt;none&amp;gt;", choices)

raw = dbutils.widgets.get("My widget")
selection_list = [s for s in raw.split(",") if s and s != "&amp;lt;none&amp;gt;"]
selection = "" if not selection_list else ",".join(selection_list)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;Option 3: Stop resetting the default to the first value.&lt;/P&gt;
&lt;P class="p1"&gt;This line guarantees a selection every time:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;dbutils.widgets.multiselect("My widget", widget_values[0], widget_values)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p1"&gt;If your goal is “blank unless chosen,” this works against you.&lt;/P&gt;
&lt;P class="p1"&gt;A few extra notes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;If Run All bites you, run the reset/create cells manually or move to ipywidgets where state behaves like you expect.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Widgets speak strings only; multiselects always require you to parse the comma-delimited output.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="p1"&gt;Notebook jobs ignore whatever’s in the UI—they start with defaults unless you pass overrides.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="p1"&gt;Hope this helps, Louis.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Dec 2025 12:39:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-stop-databricks-retaining-widget-selection-between-runs/m-p/141663#M51783</guid>
      <dc:creator>Louis_Frolio</dc:creator>
      <dc:date>2025-12-11T12:39:58Z</dc:date>
    </item>
  </channel>
</rss>

