diff --git a/pkg/iam/organization_service.go b/pkg/iam/organization_service.go index 0c2f632d3..dca117ffa 100644 --- a/pkg/iam/organization_service.go +++ b/pkg/iam/organization_service.go @@ -1580,12 +1580,23 @@ func (s OrganizationService) DeleteSCIMConfiguration( } if err == nil { - // Bridge exists, delete connector if it has one + // Bridge exists. Only delete the underlying connector if nothing + // else references it (e.g. access_sources). Otherwise leave it in + // place — the bridge's FK is ON DELETE SET NULL, so deleting the + // bridge alone is sufficient to unbind SCIM from the connector. if bridge.ConnectorID != nil { - connector := &coredata.Connector{ID: *bridge.ConnectorID} - err = connector.Delete(ctx, tx, scope) - if err != nil && err != coredata.ErrResourceNotFound { - return fmt.Errorf("cannot delete connector: %w", err) + accessSources := &coredata.AccessSources{} + count, err := accessSources.CountByConnectorID(ctx, tx, scope, *bridge.ConnectorID) + if err != nil { + return fmt.Errorf("cannot count access sources for connector: %w", err) + } + + if count == 0 { + connector := &coredata.Connector{ID: *bridge.ConnectorID} + err = connector.Delete(ctx, tx, scope) + if err != nil && err != coredata.ErrResourceNotFound { + return fmt.Errorf("cannot delete connector: %w", err) + } } }