Managing st.session_state lifecycle with @st.cache_data TTL
26.5K reputation · 13 Oct 2025, 02:06 UTC
Managing st.session_state lifecycle with @st.cache_data TTL
Goal: ensure that any st.session_state assignments made inside a @st.cache_data‑decorated function are removed when the cache entry expires, preventing stale state from persisting across reruns.
Constraint: the current Streamlit implementation does not automatically clear session_state set within cached functions, even when the cache is invalidated by TTL or clear_cache().
Uncertainty: whether a configuration flag or API exists to bind session_state lifecycle to cache expiration, or if manual cleanup is the only supported approach.
Questions:
- Is there a Streamlit configuration or API option to automatically delete session_state keys created inside a cached function when its cache entry expires?
- If automatic clearing is not provided, what is the recommended pattern for manually clearing such session_state values without interfering with other widget state?