title: flask: `@after_this_request` not working
body: <p>I want to delete a file after the user downloaded a file which was created by the flask app.</p>
  
  <p>For doing so I found this <a href="https://stackoverflow.com/a/24613980/3991125">answer on SO</a> which did not work as expected and raised an error telling that <code>after_this_request</code> is not defined.</p>
  
  <p>Due to that I had a deeper look into <a href="http://flask.pocoo.org/snippets/53/" rel="nofollow noreferrer">Flask's documentation providing a sample snippet</a> about how to use that method. So, I extended my code by defining a <code>after_this_request</code> function as shown in the sample snippet.</p>
  
  <p>Executing the code resp. running the server works as expected. However, the file is not removed because <code>@after_this_request</code> is not called which is obvious since <code>After request ...</code> is not printed to Flask's output in the terminal:</p>
  
  <pre><code>#!/usr/bin/env python3
  # coding: utf-8
  
  
  import os
  from operator import itemgetter
  from flask import Flask, request, redirect, url_for, send_from_directory, g
  from werkzeug.utils import secure_filename
  
  UPLOAD_FOLDER = '.'
  ALLOWED_EXTENSIONS = set(['csv', 'xlsx', 'xls'])
  
  app = Flask(__name__)
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  
  
  def allowed_file(filename):
      return '.' in filename and \
             filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  
  
  def after_this_request(func):
      if not hasattr(g, 'call_after_request'):
          g.call_after_request = []
      g.call_after_request.append(func)
      return func
  
  
  @app.route('/', methods=['GET', 'POST'])
  def upload_file():
      if request.method == 'POST':
          if 'file' not in request.files:
              flash('No file part')
              return redirect(request.url)
          file = request.files['file']
          if file.filename == '':
              flash('No selected file')
              return redirect(request.url)
          if file and allowed_file(file.filename):
              filename = secure_filename(file.filename)
              filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
              file.save(filepath)
  
              @after_this_request
              def remove_file(response):
                  print('After request ...')
                  os.remove(filepath)
                  return response
  
              return send_from_directory('.', filename=filepath, as_attachment=True)
  
      return '''
      &lt;!doctype html&gt;
      &lt;title&gt;Upload a file&lt;/title&gt;
      &lt;h1&gt;Uplaod new file&lt;/h1&gt;
      &lt;form action="" method=post enctype=multipart/form-data&gt;
        &lt;p&gt;&lt;input type=file name=file&gt;
           &lt;input type=submit value=Upload&gt;
      &lt;/form&gt;
      '''
  
  
  if __name__ == '__main__':
      app.run(host='0.0.0.0', port=8080, debug=True)
  </code></pre>
  
  <p>What do I miss here? How can I ensure calling the function following to the <code>@after_this_request</code> decorator in order to delete the file after it was downloaded by the user?</p>
  
  <p><em>Note: Using Flask version 0.11.1</em></p>