CICD-277 · Delete ephemeral environment
Encyclopedia · Advanced Kubernetes operations · All snippets
Previous · CICD-276 · Next · CICD-278
1. Where it fits
- Placement CI run step or authorized lab terminal.
- Prerequisites kubectl, isolated cluster or namespace and installed CRDs when named.
- Platforms kubernetes, local-lab.
- Parameters
PR_NUMBER.
2. Copy the snippet
bash
[[ "${PR_NUMBER:?}" =~ ^[0-9]+$ ]]
name="preview-$PR_NUMBER"
test "$(kubectl get namespace "$name" -o jsonpath='{.metadata.labels.managed-by}')" = preview
kubectl delete namespace "$name" --wait=false3. How it works
The generated namespace name is validated and its ownership label is checked before deletion. The command requests teardown and returns before finalizers finish.
4. Verify the result
Review the namespace ownership and disposability before deletion. Inspect finalizers if termination does not complete.
Watch for. Use an independently reviewed target, owner and scoped write identity. Confirm the resulting state separately.
Validation syntax-validated (Bash parse). Status definitions.