pythonsqlalchemyflask-sqlalchemy

Flask-SQLAlchemy how to delete all rows in a single table


How do I delete all rows in a single table using Flask-SQLAlchemy?

Looking for something like this:

>>> users = models.User.query.all()
>>> models.db.session.delete(users)

# but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not mapped

Solution

  • Try delete:

    models.User.query.delete()
    

    From the docs: Returns the number of rows deleted, excluding any cascades.