Skip to main content

Delete models with SQLAlchemy

Just as with adding, deleting models is a matter of using db.session, and then committing when the deletion is complete:

resources/item.py
def delete(self, item_id):
item = ItemModel.query.get_or_404(item_id)
db.session.delete(item)
db.session.commit()
return {"message": "Item deleted."}
resources/store.py
def delete(self, store_id):
store = StoreModel.query.get_or_404(store_id)
db.session.delete(store)
db.session.commit()
return {"message": "Store deleted"}, 200