Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rm.go #5501

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions cli/command/container/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type rmOptions struct {
rmVolumes bool
rmLink bool
force bool
all bool // <-- Added this field for the --all option

containers []string
}
Expand Down Expand Up @@ -45,12 +46,31 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
flags.BoolVarP(&opts.rmVolumes, "volumes", "v", false, "Remove anonymous volumes associated with the container")
flags.BoolVarP(&opts.rmLink, "link", "l", false, "Remove the specified link")
flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of a running container (uses SIGKILL)")
flags.BoolVarP(&opts.all, "all", "a", false, "Remove all containers (both running and stopped)") // <-- Added this line

return cmd
}

func runRm(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error {
var errs []string
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, ctrID string) error {
var containersToRemove []string

// If --all is specified, fetch all container IDs
if opts.all {
// Use Docker API to get all containers (use the appropriate options)
containers, err := dockerCli.Client().ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return err
}
for _, ctr := range containers {
containersToRemove = append(containersToRemove, ctr.ID) // Add container ID to the list
}
} else {
containersToRemove = opts.containers // Use specified containers
}

// Use the same parallel operation for removing containers
errChan := parallelOperation(ctx, containersToRemove, func(ctx context.Context, ctrID string) error {
ctrID = strings.Trim(ctrID, "/")
if ctrID == "" {
return errors.New("Container name cannot be empty")
Expand All @@ -62,7 +82,7 @@ func runRm(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error {
})
})

for _, name := range opts.containers {
for _, name := range containersToRemove {
if err := <-errChan; err != nil {
if opts.force && errdefs.IsNotFound(err) {
fmt.Fprintln(dockerCli.Err(), err)
Expand Down