SQLAlchemy: How do you delete multiple rows without querying
<p>I have a table that has millions of rows. I want to delete multiple rows via an in clause.  However, using the code:</p>

<pre><code>session.query(Users).filter(Users.id.in_(subquery....)).delete()
</code></pre>

<p>The above code will query the results, and then execute the delete.  I don't want to do that. I want speed.</p>

<p>I want to be able to execute (yes I know about the session.execute):<code>Delete from users where id in ()</code></p>

<p><strong>So the Question:</strong> How can I get the best of two worlds, using the ORM? Can I do the delete without hard coding the query?</p>

<p>Yep! You can call <code>delete()</code> on the table object with an associated whereclause. </p>

<p>Something like this:</p>

<p><code>stmt = Users.__table__.delete().where(Users.id.in_(subquery...))</code></p>

<p>(and then don't forget to execute the statement: <code>engine.execute(stmt)</code>)</p>

<p><a href="http://docs.sqlalchemy.org/en/latest/core/selectable.html#sqlalchemy.sql.expression.TableClause.delete" rel="nofollow">source</a></p>

python orm sqlalchemy