diff --git a/completion/bash/podman-compose b/completion/bash/podman-compose index ffcdba6..4f62e91 100644 --- a/completion/bash/podman-compose +++ b/completion/bash/podman-compose @@ -98,7 +98,7 @@ _completeExecArgs() { # complete the arguments for `podman-compose down` and return 0 _completeDownArgs() { - down_opts="${help_opts} -v --volumes -t --timeout" + down_opts="${help_opts} -v --volumes -t --timeout --remove-orphans" if [[ ${prev} == "-t" || ${prev} == "--timeout" ]]; then return 0 elif [[ ${cur} == -* ]]; then diff --git a/podman_compose.py b/podman_compose.py index 23ebe16..d61a40e 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2141,6 +2141,26 @@ def compose_down(compose, args): if cnt["_service"] in excluded: continue compose.podman.run([], "rm", [cnt["name"]], sleep=0) + if args.remove_orphans: + names = ( + compose.podman.output( + [], + "ps", + [ + "--filter", + f"label=io.podman.compose.project={compose.project_name}", + "-a", + "--format", + "{{ .Names }}", + ], + ) + .decode("utf-8") + .splitlines() + ) + for name in names: + compose.podman.run([], "stop", [*podman_args, name], sleep=0) + for name in names: + compose.podman.run([], "rm", [name], sleep=0) if args.volumes: vol_names_to_keep = set() for cnt in containers: @@ -2564,6 +2584,11 @@ def compose_down_parse(parser): help="Remove named volumes declared in the `volumes` section of the Compose file and " "anonymous volumes attached to containers.", ) + parser.add_argument( + "--remove-orphans", + action="store_true", + help="Remove containers for services not defined in the Compose file.", + ) @cmd_parse(podman_compose, "run")