Clear the cache for file uploder on streamlit

Hello, I have a big issue when I try to upload a batch of images using file uploader for the first batch is upload but I need to clear the list of file uploader to classify the other batches because if I don’t the second, third,… just append How can I fix this issue???

Hey @safae_belkyr,

First, welcome to the Streamlit community!!! :partying_face: :partying_face: :tada: :tada: :partying_face: :star_struck:

The only way to clear files from the uploader is to hit the ‘x’ button, there is no official programmatic way to clear them.

BUT with the introduction of forms and the clear_on_submit, you might be able to get the behaviour your looking for! :raised_hands:

Take this example:

with st.form("my-form", clear_on_submit=True):
        file = st.file_uploader("FILE UPLOADER")
        submitted = st.form_submit_button("UPLOAD!")

    if submitted and file is not None:
        st.write("UPLOADED!")
        # do stuff with your uploaded file

This will allow you to upload a file and hit the UPLOAD! button. When Streamlit re-runs, the file will be uploaded and run through any process you have in the if submitted and file is not None statement, and the file_uploader at the top will be cleared and ready for the next file or “batch”!

Happy Streamlit-ing!
Marisa

9 Likes

Thanks, this is how the widget should be out of the box, this is great :slight_smile:

2 Likes

This is still bad because it still holds in memory for whatever reason…

@Marisa_Smith
I was wondering if you could kindly clarify whether the only way to remove/clear uploaded file(s) is still by hitting the ‘x’ button or if there is an official programmatic way to clear them. The reason I ask is because I have created a streamlit button called "Reset all" where this button will make the app return to the state of when the program just successfully executed via streamlit run app.py.

That’s mean the program is returning to the beginning stage without any user input.

This way is not work when I want to put the file_uploader widget in the st.sidebar. The error is as following:

StreamlitAPIException: st.form_submit_button() must be used inside an st.form().

For more information, refer to the documentation for forms.