flask remove file after send_file

from flask import Flask, send_file, after_this_request
import os

app = Flask(__name__)

@app.route('/download')
def download_file():
    # Specify the file path
    file_path = 'path/to/your/file.txt'

    # Define a function to be executed after the file is sent
    @after_this_request
    def remove_file(response):
        try:
            # Remove the file after it has been sent
            os.remove(file_path)
        except Exception as error:
            app.logger.error("Error removing file: %s", error)
        return response

    # Send the file to the client
    return send_file(file_path, as_attachment=True)

if __name__ == '__main__':
    app.run(debug=True)