Skip to content

'docker rm --force' to succeed when containers already deleted #2383

@mleonhard

Description

@mleonhard

Desired Behavior

I need the docker rm --force command to exit with success if the specified containers don't exist. This is analogous to the rm -f shell command.

$ docker rm --force nonexistent_container ; echo $?
0

Current Behavior

Currently, the command exits with status 1 if no containers were deleted.

$ docker rm --force nonexistent_container ; echo $?
Error: No such container: nonexistent_container
1

Why I need this change

I am calling this command in a cleanup script. I need to be able to run the script repeatedly, without creating any containers in between runs. The script needs to stop and report an error if docker rm --force cannot do what it needs to do. For example, if it cannot connect to dockerd. But if the containers are already deleted, then the script needs to continue. With the current behavior of docker rm --force, the script must perform its own check for containers before calling docker rm --force. This is extra code. Alternatively, the script can silently ignore failure of the command. This has serious drawbacks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions