This comes up a lot with chat layouts and with spinner/progress elements. You can use containers and/or empties to clear the elements. Here’s another thread about it. (Not exactly the same scenario. Let me know if it’s not clear.) Streamlit Spinner and Chat Message odd interaction - #2 by mathcatsand
When Streamlit reruns, it replaces elements one at a time. (The first element rendered in the rerun replaces the first element from the previous one.) During this process all other elements on the page are in a stale state while Streamlit waits to get through the script. Only at the end of the script run will it discard any leftover stale elements.
Containers and empties given you a way to ensure there is not some nth element sitting on your page during a script run, waiting to find out it’s not getting replaced.