def METHOD_NAME(session, ctx):
    # Look through Session state to see if we want to emit a DELETE for
    # orphans
    orphans_found = (
        any(
            isinstance(obj, parent_class) and
            sa.orm.attributes.get_history(obj, attr.key).deleted
            for obj in session.dirty
        ) or
        any(
            isinstance(obj, parent_class)
            for obj in session.deleted
        )
    )
    if orphans_found:
        # Emit a DELETE for all orphans
        (
            session.query(target_class)
            .filter(
                ~getattr(target_class, backref).any()
            )
            .delete(synchronize_session=False)
        )