Skip to content

CRUD - Delete Record

Deleting records should be done carefully (data loss is permanent).

Example

with app.app_context():
    user = User.query.get(1)
    if user:
        db.session.delete(user)
        db.session.commit()
with app.app_context():
    user = User.query.get(1)
    if user:
        db.session.delete(user)
        db.session.commit()

Soft deletes (common pattern)

Many applications avoid hard delete.

Instead they add a flag:

  • is_deletedis_deleted

And filter it out by default.

This helps:

  • auditing
  • recovery

Cascades

If other tables depend on the record (foreign keys), deleting may:

  • fail due to integrity constraints, or
  • delete child records (if cascade is configured)

Understand your relationships before enabling cascade delete.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did