Remove old images by ID after container updates, not just dangling prune

Tagged images are never caught by docker image prune -f. Collect OLD_IDs
during the pull loop and explicitly rmi them after rebuilds complete.
This commit is contained in:
Gmer4Lfe
2026-06-07 09:54:42 -04:00
parent 1e54752478
commit 819f5e6450
2 changed files with 16 additions and 5 deletions
+6 -1
View File
@@ -166,6 +166,7 @@ START=$(date +%s)
UPDATED=()
UP_TO_DATE=()
FAILED=()
OLD_IMAGE_IDS=()
for container in "${REMAINING[@]}"; do
[[ -z "$container" ]] && continue
@@ -202,6 +203,7 @@ for container in "${REMAINING[@]}"; do
if [[ -n "$OLD_ID" ]] && [[ "$OLD_ID" != "$NEW_ID" ]]; then
log "$ICON_DONE $container — updated ✅ (${OLD_ID:7:12}${NEW_ID:7:12})"
UPDATED+=("$container")
OLD_IMAGE_IDS+=("$OLD_ID")
else
log "$container — already up to date (${NEW_ID:7:12})"
UP_TO_DATE+=("$container")
@@ -269,9 +271,12 @@ echo ""
echo "━━━ $ICON_SYNC Pruning Dangling Images — $(date '+%Y-%m-%d %H:%M:%S') ━━━"
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — would prune dangling images"
warn "DRY RUN — would remove ${#OLD_IMAGE_IDS[@]} old image(s) and prune dangling"
PRUNED_SUMMARY="(dry run)"
else
for _old_id in "${OLD_IMAGE_IDS[@]}"; do
docker rmi "$_old_id" >/dev/null 2>&1 || true
done
PRUNED_OUTPUT=$(docker image prune -f 2>&1)
[[ "$ENABLE_LOGGING" == "true" ]] && echo "$PRUNED_OUTPUT" | sed 's/^/ /'
PRUNED_SUMMARY=$(echo "$PRUNED_OUTPUT" | grep -E "^Total reclaimed" || echo "nothing reclaimed")