#!/usr/bin/env bash
set -euo pipefail

for phase in Succeeded Failed; do
  kubectl get pods -A --field-selector="status.phase=${phase}" \
    -o jsonpath='{range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}' \
    | while read -r namespace name; do
        if [ -n "${namespace}" ] && [ -n "${name}" ]; then
          kubectl delete pod -n "${namespace}" "${name}" --ignore-not-found --grace-period=0 --wait=false
        fi
      done
done
