How to remove the uploaded file

Dear all,

I hope this message finds you well.

I am currently developing a chat application where users can interact with an AI model by uploading images. The image upload functionality is implemented using the following Streamlit code:

uploaded_file = st.file_uploader('Please upload an image', type=['jpg', 'jpeg', 'png'])

This setup allows the uploaded image to be used as user input, and the AI model processes it to generate responses.

However, I am facing an issue when attempting to upload multiple images sequentially. Once an image is uploaded, the system continues to treat it as an active user input, preventing new uploads from being processed correctly.

To address this issue, I need a mechanism to either remove or erase the previously uploaded image from the session state, so that the user can upload a new image without interference. Here is the relevant portion of my current implementation:

if 'uploaded_image' not in st.session_state:
    st.session_state['uploaded_image'] = None

uploaded_file = st.file_uploader('Please upload an image', type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
    st.session_state['uploaded_image'] = uploaded_file
    # Processing the uploaded image
    # ...
    st.session_state['uploaded_image'] = None  # Attempt to reset after processing

Despite setting st.session_state['uploaded_image'] to None after processing, the issue persists.

Could you please provide guidance or suggest a better approach to effectively handle multiple image uploads in this context?

Thank you in advance for your assistance. I look forward to your response.

Best regards,

I don’t get that, could you provide a sample code?

Note, on your posted code, I don’t have issues with that, I can change an image by uploading another image file.

A cheap trick to clear any widget is to change the key, so you could so something like this

import streamlit as st
from time import sleep

if "uploader_key" not in st.session_state:
    st.session_state["uploader_key"] = 1

uploaded_file = st.file_uploader(
    "Please upload an image",
    type=["jpg", "jpeg", "png"],

if uploaded_file is not None:
    with st.spinner("Processing"):

    # Clear the file uploader
    st.session_state["uploader_key"] += 1

The following is the best way to upload using “streamlit forms”. This allows clear_on_submit and this is what possibly you are looking for. This along with the tip from Blackary may be able to solve your issue. Good luck!

NOTE: In this way, the user first uploads the file by clicking “Select a file for upload” button. After upload, he still needs to click “Upload the selected file”. It is a 2-step process.

with upload_column:
    with st.form(APP_ID + "/upload_form", clear_on_submit=True):
        uploaded_file = st.file_uploader('Select a file for upload', type=['xls', 'xlsx'], key=APP_ID + '/file_uploader')
        file_submitted = st.form_submit_button("Upload the selected file!")
        if file_submitted and (uploaded_file is not None):
            <perform upload related task>

Dear blackary

Awesome, code ! I sovled the problem using another args “uploader_key”


Best regards
Wonjoong Cheon

Dear Sarnath_K

Thks for sharing the best way to solve my problem.

Best regards
Wonjoong Cheon

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.