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)
| 3303 | state._detach(self) |
| 3304 | |
| 3305 | def expunge(self, instance: object) -> None: |
| 3306 | """Remove the `instance` from this ``Session``. |
| 3307 | |
| 3308 | This will free all internal references to the instance. Cascading |
| 3309 | will be applied according to the *expunge* cascade rule. |
| 3310 | |
| 3311 | """ |
| 3312 | try: |
| 3313 | state = attributes.instance_state(instance) |
| 3314 | except exc.NO_STATE as err: |
| 3315 | raise exc.UnmappedInstanceError(instance) from err |
| 3316 | if state.session_id is not self.hash_key: |
| 3317 | raise sa_exc.InvalidRequestError( |
| 3318 | "Instance %s is not present in this Session" % state_str(state) |
| 3319 | ) |
| 3320 | |
| 3321 | cascaded = list( |
| 3322 | state.manager.mapper.cascade_iterator("expunge", state) |
| 3323 | ) |
| 3324 | self._expunge_states([state] + [st_ for o, m, st_, dct_ in cascaded]) |
| 3325 | |
| 3326 | def _expunge_states( |
| 3327 | self, states: Iterable[InstanceState[Any]], to_transient: bool = False |