diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index 520eddb26..3b1966248 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -105,7 +105,7 @@ The following list config variables have special meaning and can be set in a var
| `DOKKU_NGINX_SSL_PORT` | automatically assigned | `dokku config:set` | |
| `DOKKU_PROXY_PORT_MAP` | automatically assigned | `dokku proxy:ports-add`
`dokku proxy:ports-remove`, `dokku proxy:ports-clear` | |
| `DOKKU_SKIP_ALL_CHECKS` | none | `dokku config:set` | |
-| `DOKKU_SKIP_CLEANUP` | | `/etc/environment`
`~dokku/.dokkurc`
`~dokku/.dokkurc/*` | |
+| `DOKKU_SKIP_CLEANUP` | | `/etc/environment`
`~dokku/.dokkurc`
`~dokku/.dokkurc/*` | When a deploy is triggered, if this is set to a non-empty value, then old docker containers and images will not be removed. |
| `DOKKU_SKIP_DEFAULT_CHECKS` | | `dokku config:set` | |
| `DOKKU_SKIP_DEPLOY` | | `dokku config:set` | |
| `DOKKU_WAIT_TO_RETIRE` | `60` | `dokku config:set` | After a successful deploy, the grace period given to old containers before they are stopped/terminated. This is useful for ensuring completion of long-running http connections. |
diff --git a/plugins/00_dokku-standard/subcommands/cleanup b/plugins/00_dokku-standard/subcommands/cleanup
index 69aa0503f..6403f6bdb 100755
--- a/plugins/00_dokku-standard/subcommands/cleanup
+++ b/plugins/00_dokku-standard/subcommands/cleanup
@@ -6,7 +6,7 @@ dokku_cleanup_cmd(){
declare desc="cleans up old deployment cruft"
local cmd="cleanup"
- docker_cleanup
+ docker_cleanup "$APP" "true"
}
diff --git a/plugins/common/functions b/plugins/common/functions
index c4faee173..0f483f32a 100755
--- a/plugins/common/functions
+++ b/plugins/common/functions
@@ -746,12 +746,8 @@ dokku_receive() {
source "$PLUGIN_AVAILABLE_PATH/config/functions"
local APP="$1"; local IMAGE=$(get_app_image_name "$APP"); local IMAGE_SOURCE_TYPE="$2"; local TMP_WORK_DIR="$3"
- if [[ -z "$DOKKU_SKIP_CLEANUP" ]]; then
- dokku_log_info1 "Cleaning up..."
- docker_cleanup
- else
- dokku_log_info1 "DOKKU_SKIP_CLEANUP set. Skipping dokku cleanup"
- fi
+ local DOKKU_SKIP_CLEANUP="$(config_get "$APP" DOKKU_SKIP_CLEANUP || true)"
+ docker_cleanup "$APP"
dokku_log_info1 "Building $APP from $IMAGE_SOURCE_TYPE..."
config_set --no-restart "$APP" DOKKU_APP_TYPE="$IMAGE_SOURCE_TYPE" &> /dev/null
dokku_build "$APP" "$IMAGE_SOURCE_TYPE" "$TMP_WORK_DIR"
@@ -760,6 +756,19 @@ dokku_receive() {
docker_cleanup() {
declare desc="cleans up all exited/dead containers and removes all dangling images"
+ declare APP="$1" FORCE_CLEANUP="$2"
+ local DOKKU_APP_SKIP_CLEANUP
+
+ if [[ "$FORCE_CLEANUP" != "true" ]]; then
+ [[ -z "$APP" ]] && DOKKU_APP_SKIP_CLEANUP="$(config_get "$APP" DOKKU_SKIP_CLEANUP || true)"
+ if [[ -n "$DOKKU_SKIP_CLEANUP" ]] || [[ -n "$DOKKU_APP_SKIP_CLEANUP" ]]; then
+ dokku_log_info1 "DOKKU_SKIP_CLEANUP set. Skipping dokku cleanup"
+ return
+ fi
+ fi
+
+ dokku_log_info1 "Cleaning up..."
+
# delete all non-running containers
# shellcheck disable=SC2046
docker rm $(docker ps -a -f "status=exited" -f "label=$DOKKU_CONTAINER_LABEL" -q) &> /dev/null || true