How to delete form and free it's key

I created a form in a loop, after each run of the loop, I want to delete the form and free its key, so I can use the same key again next time? Instead of giving a different key every time, creating a whole bunch of forms, and eventually using up all the memory. Is that possible to delete the form completely?

for apple in apples:
with st.form(key = “the_form”, clear_on_submit=False, border=False):

delete st.form?

Now it complains the key is already used.

Could you be more precise about your use case ?
It is indeed not possible to have multiple forms with the same key. However, the form does not take space in the session state. So it should not use all the memory. If you want, you can create a callback to delete the inputs that were set in the form.

To be sure, you want to display multiple forms at the same time or wait for the user to submit to display the next form ?

Hello and sorry for the delay ! Do we agree that when a user input a new request, the old form is overwritten and a new one is created instead ?

Yes, I agree with that. I know I can reuse the old form, but I wonder if it’s possible to delete it completely. If I really want to delete a form, free its key. Is that possible? How should I do that?

You don’t have to do anything, that happens automatically.

1 Like

It seems that when I try to use the same key, I get an error saying that I cannot use a duplicate key. I just want to delete it and free the key, similar to garbage collection.

Are you trying to reuse a key in the same rerun? You cannot do that.