title: How to delete files from the server with Flask
body: <p>When i upload files with this function: </p>
  
  <pre><code>@app.route('/add_item', methods=['GET', 'POST'])
  @login_required
  def new_item():
      error = None
      form = AddItemForm(request.form)
      if request.method == 'POST':
          file = request.files['file']
          if file and allowed_file(file.filename) and form.name.data != "" and form.description.data != "":
              filename = secure_filename(file.filename)
              file.save(os.path.join(app.config['UPLOADED_ITEMS_DEST'], filename))
              new_item = Item(
                  filename,
                  form.name.data,
                  form.description.data,
                  form.price.data,
                  form.age.data,
                  form.particles.data,
                  form.category.data,
                  '1',
                  )
              db.session.add(new_item)
              db.session.commit()
              return redirect(url_for('admin_items'))
          else:
              return render_template('admin_items.html', form=form, error=error)
      if request.method == 'GET':
          return redirect(url_for('admin_items'))
  </code></pre>
  
  <p>How can i delete this uploaded file with the function which deletes the item? The issue is that the function i currently have only deletes the content of the item (description, price, etc) but the actual file which was uploaded to the folder is not removed of course! This creates a problem! </p>
  
  <p>Here is my delete function:</p>
  
  <pre><code># Delete Items:
  @app.route('/delete_item/&lt;int:item_id&gt;/', methods=['GET', 'POST'])
  @login_required
  def delete_item(item_id):
      new_id = item_id
      os.remove(os.path.join(app.config['UPLOADED_ITEMS_DEST'], filename))
      db.session.query(Item).filter_by(item_id=new_id).delete()
      db.session.commit()
  
      return redirect(url_for('admin_items'))
  </code></pre>