MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / delete

Method delete

lib/sqlalchemy/orm/query.py:3156–3229  ·  view source on GitHub ↗

r"""Perform a DELETE with an arbitrary WHERE clause. Deletes rows matched by this query from the database. E.g.:: sess.query(User).filter(User.age == 25).delete(synchronize_session=False) sess.query(User).filter(User.age == 25).delete( sync

(
        self,
        synchronize_session: SynchronizeSessionArgument = "auto",
        delete_args: Optional[Dict[Any, Any]] = None,
    )

Source from the content-addressed store, hash-verified

3154 )
3155
3156 def delete(
3157 self,
3158 synchronize_session: SynchronizeSessionArgument = "auto",
3159 delete_args: Optional[Dict[Any, Any]] = None,
3160 ) -> int:
3161 r"""Perform a DELETE with an arbitrary WHERE clause.
3162
3163 Deletes rows matched by this query from the database.
3164
3165 E.g.::
3166
3167 sess.query(User).filter(User.age == 25).delete(synchronize_session=False)
3168
3169 sess.query(User).filter(User.age == 25).delete(
3170 synchronize_session="evaluate"
3171 )
3172
3173 .. warning::
3174
3175 See the section :ref:`orm_expression_update_delete` for important
3176 caveats and warnings, including limitations when using bulk UPDATE
3177 and DELETE with mapper inheritance configurations.
3178
3179 :param synchronize_session: chooses the strategy to update the
3180 attributes on objects in the session. See the section
3181 :ref:`orm_expression_update_delete` for a discussion of these
3182 strategies.
3183
3184 :param delete_args: Optional dictionary, if present will be passed
3185 to the underlying :func:`_expression.delete` construct as the ``**kw``
3186 for the object. May be used to pass dialect-specific arguments such
3187 as ``mysql_limit``.
3188
3189 .. versionadded:: 2.0.37
3190
3191 :return: the count of rows matched as returned by the database's
3192 "row count" feature.
3193
3194 .. seealso::
3195
3196 :ref:`orm_expression_update_delete`
3197
3198 """ # noqa: E501
3199
3200 bulk_del = BulkDelete(self, delete_args)
3201 if self.dispatch.before_compile_delete:
3202 for fn in self.dispatch.before_compile_delete:
3203 new_query = fn(bulk_del.query, bulk_del)
3204 if new_query is not None:
3205 bulk_del.query = new_query
3206
3207 self = bulk_del.query
3208
3209 delete_ = sql.delete(*self._raw_columns) # type: ignore
3210
3211 if delete_args:
3212 delete_ = delete_.with_dialect_options(**delete_args)
3213

Callers 15

_run_crudMethod · 0.45
delete_stmtFunction · 0.45
delete_from_all_tablesFunction · 0.45
test_delete_singleMethod · 0.45
test_delete_manyMethod · 0.45
test_deleteMethod · 0.45
test_delete_returningMethod · 0.45
runMethod · 0.45

Calls 7

BulkDeleteClass · 0.85
castFunction · 0.85
with_dialect_optionsMethod · 0.80
after_bulk_deleteMethod · 0.80
executeMethod · 0.45
unionMethod · 0.45
closeMethod · 0.45