Remove the `instance` from this ``Session``. This will free all internal references to the instance. Cascading will be applied according to the *expunge* cascade rule.
(self, instance: object)
| 3405 | state._detach(self) |
| 3406 | |
| 3407 | def expunge(self, instance: object) -> None: |
| 3408 | """Remove the `instance` from this ``Session``. |
| 3409 | |
| 3410 | This will free all internal references to the instance. Cascading |
| 3411 | will be applied according to the *expunge* cascade rule. |
| 3412 | |
| 3413 | """ |
| 3414 | try: |
| 3415 | state = attributes.instance_state(instance) |
| 3416 | except exc.NO_STATE as err: |
| 3417 | raise exc.UnmappedInstanceError(instance) from err |
| 3418 | if state.session_id is not self.hash_key: |
| 3419 | raise sa_exc.InvalidRequestError( |
| 3420 | "Instance %s is not present in this Session" % state_str(state) |
| 3421 | ) |
| 3422 | |
| 3423 | cascaded = list( |
| 3424 | state.manager.mapper.cascade_iterator("expunge", state) |
| 3425 | ) |
| 3426 | self._expunge_states([state] + [st_ for o, m, st_, dct_ in cascaded]) |
| 3427 | |
| 3428 | def _expunge_states( |
| 3429 | self, states: Iterable[InstanceState[Any]], to_transient: bool = False |