How to remove files created in the app when the user

I would like to remove all files created by the user in my streamlit app when the user closes the browser. I have been using atexit.register(), but this only cleans up my files when the app is stopped from the Python interpreter. This doesn’t work because the user doesn’t stop the Python process, he/she just closes the browser and (2) my app is deployed on Azure and there the Python process is not stopped when the user exits the app.

Could it be the tempfile module what you are looking for?

1 Like

I found a workaround: I save all files in a folder and remove the folder every time at the bottom of the script. Since the creation of the files happens very fast, I don’t need to wait until the user closes the browser.

import pandas as pd
import shutil

if not os.path.exists(created_files_path):
    os.makedirs(created_files_path)

with pd.ExcelWriter(created_files_path) as writer_object:
        df.to_excel(
            writer_object, index=False
        )

shutil.rmtree(created_files_path)

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