Search code examples
pythoncachingstreamlit

how can I clear specific streamlit cache?


I run streamlit script with a few caches.

When I use the following code it clears all the caches:

from streamlit import caching

caching.clear_cache()

I would like to clear only a specific cache. How can I do this?


Solution

  • This isn’t currently (easily) doable.

    This is an optional solution that can be applied to some cases:

    You can use the allow_output_mutation option:

    import streamlit as st
    
    @st.cache(allow_output_mutation=True)
    def mutable_cache():
        return some_list
    
    mutable_object = mutable_cache()
    
    if st.button("Clear history cache"):
        mutable_object.clear()
    

    I wrote the returned cached object as list but you can use other object types as well (then you have to replace the clear method which is specific to lists).

    For more info please look on the answers I got in the streamlit community forum