Flask-SQLAlchemy how to delete all rows in a single table
Solution 1:
Try delete
:
models.User.query.delete()
From the docs: Returns the number of rows deleted, excluding any cascades.
Solution 2:
DazWorrall's answer is spot on. Here's a variation that might be useful if your code is structured differently than the OP's:
num_rows_deleted = db.session.query(Model).delete()
Also, don't forget that the deletion won't take effect until you commit, as in this snippet:
try:
num_rows_deleted = db.session.query(Model).delete()
db.session.commit()
except:
db.session.rollback()