Skip to content

Commit b4c0792

Browse files
tjikkunmuayyad-alsadi
authored andcommittedApr 9, 2023
Add --remove-orphans on down command
Signed-off-by: Sander Hoentjen <[email protected]>
1 parent e84451f commit b4c0792

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎completion/bash/podman-compose

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ _completeExecArgs() {
9898

9999
# complete the arguments for `podman-compose down` and return 0
100100
_completeDownArgs() {
101-
down_opts="${help_opts} -v --volumes -t --timeout"
101+
down_opts="${help_opts} -v --volumes -t --timeout --remove-orphans"
102102
if [[ ${prev} == "-t" || ${prev} == "--timeout" ]]; then
103103
return 0
104104
elif [[ ${cur} == -* ]]; then

‎podman_compose.py

+25
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,26 @@ def compose_down(compose, args):
21412141
if cnt["_service"] in excluded:
21422142
continue
21432143
compose.podman.run([], "rm", [cnt["name"]], sleep=0)
2144+
if args.remove_orphans:
2145+
names = (
2146+
compose.podman.output(
2147+
[],
2148+
"ps",
2149+
[
2150+
"--filter",
2151+
f"label=io.podman.compose.project={compose.project_name}",
2152+
"-a",
2153+
"--format",
2154+
"{{ .Names }}",
2155+
],
2156+
)
2157+
.decode("utf-8")
2158+
.splitlines()
2159+
)
2160+
for name in names:
2161+
compose.podman.run([], "stop", [*podman_args, name], sleep=0)
2162+
for name in names:
2163+
compose.podman.run([], "rm", [name], sleep=0)
21442164
if args.volumes:
21452165
vol_names_to_keep = set()
21462166
for cnt in containers:
@@ -2564,6 +2584,11 @@ def compose_down_parse(parser):
25642584
help="Remove named volumes declared in the `volumes` section of the Compose file and "
25652585
"anonymous volumes attached to containers.",
25662586
)
2587+
parser.add_argument(
2588+
"--remove-orphans",
2589+
action="store_true",
2590+
help="Remove containers for services not defined in the Compose file.",
2591+
)
25672592

25682593

25692594
@cmd_parse(podman_compose, "run")

0 commit comments

Comments
 (0)
Please sign in to comment.