title: How can I generate file on the fly and delete it after download?
body: <p>here's my function that creates file on the fly(when the user clicks proper link)</p>
  
  <pre><code>@app.route('/survey/&lt;survey_id&gt;/report')
  def survey_downloadreport(survey_id):
      survey, bsonobj = survey_get(survey_id) #get object
      resps = response_get_multi(survey_id) #get responses to the object
  
      fields = ["_id", "sid", "date", "user_ip"] #meta-fields
      fields.extend(survey.formfields) #survey-specific fields
  
      randname = "".join(random.sample(string.letters + string.digits, 15)) + ".csv" #some random file name
  
      with open("static//" + randname, "wb") as csvf:
          wr = csv.DictWriter(csvf, fields, encoding = 'cp949')
          wr.writerow(dict(zip(fields, fields))) #dummy, to explain what each column means
          for resp in resps :
              wr.writerow(resp)
  
      return send_from_directory("static", randname, as_attachment = True)
  </code></pre>
  
  <p>I'd like to have file to be deleted after completing of the download. How can I do it?</p>