diff --git a/lib/portlayer/network/context.go b/lib/portlayer/network/context.go index 6bc410ee00..d544e8d2e1 100644 --- a/lib/portlayer/network/context.go +++ b/lib/portlayer/network/context.go @@ -1269,6 +1269,18 @@ func (c *Context) DeleteScope(ctx context.Context, name string) error { return fmt.Errorf("cannot remove builtin scope") } + // remove dangling endpoints + if exec.Containers != nil { + for _, eps := range s.Endpoints() { + if exec.Containers.Container(eps.ID().String()) == nil { + log.Debugf("Remove dangling endpoint (%s)", eps.ID().String()) + if err = s.RemoveContainer(eps.Container()); err != nil { + return fmt.Errorf("failed to remove dangling endpoint (%s): %s", eps.ID().String(), err.Error()) + } + } + } + } + if len(s.Endpoints()) != 0 { return fmt.Errorf("%s has active endpoints", s.Name()) }