Dependent Variable Retains Previous Selection When Query Returns No Results

Variable Dropdown Retains Previous Options When Query Returns No Results (PostgreSQL)

What Grafana version and what operating system are you using?

  • Grafana: 13.x

  • Data Source: PostgreSQL

  • Operating System: Linux (Docker)

What are you trying to achieve?

I am using dependent dashboard variables where the available options in a child variable depend on the selection of a parent variable.

I expect the dropdown options to always reflect the current query result.

How are you trying to achieve it?

I have query-based PostgreSQL variables.

For example:

  • Variable A (parent)

  • Variable B (child, depends on Variable A)

When Variable A changes, Variable B is refreshed using a PostgreSQL query.

What happened?

When the child variable query returns data, the dropdown options are updated correctly.

However, if the parent variable changes and the child variable query subsequently returns no rows, the dropdown still displays the options that were returned by the previous query execution.

When I inspect the variable query in Grafana, the query result is empty and no rows are returned.

Despite this, the old options remain visible in the variable dropdown and a warning icon appears next to the variable.

It appears that the previous variable options are being preserved even though they are no longer returned by the query.

What did you expect to happen?

If the variable query returns no rows, I would expect the variable dropdown to become empty and display no available options.

I would not expect options from a previous query execution to remain visible in the dropdown.

Can you copy/paste the configuration(s) that you are having problems with?

Example query:

SELECT sensor_id
FROM sensors
WHERE node_id IN (${nodeid})
ORDER BY sensor_id

The issue occurs whenever the refreshed query returns no rows.

Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

No server-side errors appear in Grafana logs.

The variable displays a warning icon in the dashboard UI indicating that the variable state is invalid or outdated.

Did you follow any online instructions? If so, what is the URL?

No.

Additional Information

To clarify, the issue is not that the previous value remains selected.

The issue is that the previous variable options remain visible in the dropdown even though the current query result is empty.

Is this expected behavior, a caching issue, or a bug in Grafana 13.x variable handling?

This is a known Grafana behavior → when a query variable returns zero rows, Grafana retains the previous options instead of clearing the dropdown. The warning icon confirms the variable state is invalid.

Grafana does not clear variable options when the refreshed query returns no rows. It preserves the last valid result, causing stale options to persist in the dropdown.

Sentinel Value using UNION ALL
Replace your current query:
sql

SELECT sensor_id
FROM sensors
WHERE node_id IN (${nodeid})
ORDER BY sensor_id

With this:
sql

SELECT sensor_id
FROM sensors
WHERE node_id IN (${nodeid})

UNION ALL

SELECT '__no_sensors__'
WHERE NOT EXISTS (
    SELECT 1 FROM sensors WHERE node_id IN (${nodeid})
)

ORDER BY 1

This ensures the query always returns at least one row, forcing Grafana to update the dropdown instead of retaining stale options.
Then in your panels, guard against the sentinel value:
sql

WHERE (
  '${sensorid}' = '__no_sensors__'
  OR sensor_id = '${sensorid}'
)

node_B selected → both variables update correctly:

node_C selected (no sensors exist)

Before After
Dropdown Shows stale options Shows no_sensors
Variable state Invalid/broken Always valid
URL param var-sensorid= (empty) var-sensorid=no_sensors

This works by never allowing the query to return zero rows → the sentinel value __no_sensors__ acts as a clean empty state that Grafana can handle correctly.