Skip to content

Delete - Removing a Record

Deleting is the simplest operation. You only need the UUID - there are no new values to send, so there is no request body. Just a DELETE request with the id in the URL.


Sending the DELETE request

The Delete button on each row already has the data-id attribute. When clicked, you read it and send a DELETE request to /patients/ + id:

function deletePatient(button) {
    let id = button.dataset.id

    fetch("/patients/" + id, {
        method: "DELETE"
    })
        .then(response => response.json())
        .then(result => {
            loadPatients()
        })
        .catch(error => console.log("Error:", error))
}

No headers or body needed - DELETE just needs the URL.


The Flask route

@app.route('/patients/<string:id>', methods=['DELETE'])
def delete_patient(id):
    Patient.objects.filter(id=id).delete()
    return jsonify({ "status": "ok" })

filter(id=id) finds the record. .delete() removes it permanently.


One path, three methods

Notice that /patients/<string:id> now handles GET, PUT, and DELETE - each as a separate route:

@app.route('/patients/<string:id>', methods=['GET'])
def get_patient(id): ...

@app.route('/patients/<string:id>', methods=['PUT'])
def update_patient(id): ...

@app.route('/patients/<string:id>', methods=['DELETE'])
def delete_patient(id): ...

Flask looks at the method on every incoming request and calls the right function. The URL is the same. The method is what tells them apart.


The complete picture

Your app now has a full CRUD API:

Action Method URL Flask function
Add POST /patients add_patient()
List all GET /patients get_patients()
Get one GET /patients/<string:id> get_patient(id)
Update PUT /patients/<string:id> update_patient(id)
Delete DELETE /patients/<string:id> delete_patient(id)

Five routes. Four HTTP methods. Every operation covered.


Challenge

Add the DELETE route to app.py and the deletePatient() function to your template. Click Delete on a patient and watch them disappear from the table. Then restart Flask and confirm the patient is gone from the database - not just from the page.


← Update - Changing a Record    Next: Exercises →