QuerySet.delete() with related models may cause the transaction to timeout

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Gone away
    • Priority: Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: django
    • None
    • None
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      With large data sizes, the following query fails with a transactionLifetimeLimitSeconds error:

      MyModel.objects.all().delete()

      Traceback (most recent call last):
        File "/Users/nstapp/Github/django-mongodb-backend/django_mongodb_backend/query.py", line 19, in wrapper
          return func(*args, **kwargs)
        File "/Users/nstapp/Github/django-mongodb-backend/django_mongodb_backend/query.py", line 67, in delete
          return self.compiler.collection.delete_many(
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
              self.match_mql, session=self.compiler.connection.session
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ).deleted_count
          ^
        File "/Users/nstapp/Github/django-mongodb-backend/django_mongodb_backend/utils.py", line 171, in wrapper
          duration, retval = self.profile_call(func, args, kwargs)
                             ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
        File "/Users/nstapp/Github/django-mongodb-backend/django_mongodb_backend/utils.py", line 136, in profile_call
          retval = func(*args, **kwargs or {})
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/collection.py", line 1711, in delete_many
          self._delete_retryable(
          ~~~~~~~~~~~~~~~~~~~~~~^
              filter,
              ^^^^^^^
          ...<6 lines>...
              comment=comment,
              ^^^^^^^^^^^^^^^^
          ),
          ^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/collection.py", line 1587, in _delete_retryable
          return self._database.client._retryable_write(
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
              (write_concern or self.write_concern).acknowledged and not multi,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ...<2 lines>...
              operation=_Op.DELETE,
              ^^^^^^^^^^^^^^^^^^^^^
          )
          ^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/mongo_client.py", line 2061, in _retryable_write
          return self._retry_with_session(retryable, func, s, bulk, operation, operation_id)
                 ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/mongo_client.py", line 1947, in _retry_with_session
          return self._retry_internal(
                 ~~~~~~~~~~~~~~~~~~~~^
              func=func,
              ^^^^^^^^^^
          ...<4 lines>...
              operation_id=operation_id,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
          )
          ^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/_csot.py", line 125, in csot_wrapper
          return func(self, *args, **kwargs)
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/mongo_client.py", line 1993, in _retry_internal
          ).run()
            ~~~^^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/mongo_client.py", line 2730, in run
          return self._read() if self._is_read else self._write()
                                                    ~~~~~~~~~~~^^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/mongo_client.py", line 2862, in _write
          return self._func(self._session, conn, self._retryable)  # type: ignore
                 ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/collection.py", line 1572, in _delete
          return self._delete(
                 ~~~~~~~~~~~~^
              conn,
              ^^^^^
          ...<10 lines>...
              comment=comment,
              ^^^^^^^^^^^^^^^^
          )
          ^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/collection.py", line 1542, in _delete
          result = conn.command(
              self._database.name,
          ...<5 lines>...
              retryable_write=retryable_write,
          )
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/helpers.py", line 47, in inner
          return func(*args, **kwargs)
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/pool.py", line 414, in command
          return command(
              self,
          ...<20 lines>...
              write_concern=write_concern,
          )
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/synchronous/network.py", line 212, in command
          helpers_shared._check_command_response(
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
              response_doc,
              ^^^^^^^^^^^^^
          ...<2 lines>...
              parse_write_concern_error=parse_write_concern_error,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          )
          ^
        File "/Users/nstapp/Github/django-mongodb-backend-benchmark/.venv/lib/python3.13/site-packages/pymongo/helpers_shared.py", line 250, in _check_command_response
          raise OperationFailure(errmsg, code, response, max_wire_version)
      pymongo.errors.OperationFailure: operation was interrupted because the transaction exceeded the configured 'transactionLifetimeLimitSeconds', full error: {'ok': 0.0, 'errmsg': "operation was interrupted because the transaction exceeded the configured 'transactionLifetimeLimitSeconds'", 'code': 290, 'codeName': 'TransactionExceededLifetimeLimitSeconds', '$clusterTime': {'clusterTime': Timestamp(1752776485, 1), 'signature': {'hash': b'7!BX\xf7G\xb5\xa7>[_\xfausy\xab\x81\xc8?\xb9', 'keyId': 7528056412564357125}}, 'operationTime': Timestamp(1752776485, 1)}

              Assignee:
              Jib Adegunloye
              Reporter:
              Noah Stapp
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: