Skip to content

Instantly share code, notes, and snippets.

@tosin2013
Created July 1, 2024 18:53
Show Gist options
  • Save tosin2013/8fad6edaae7865325e872ac1a1f0ff05 to your computer and use it in GitHub Desktop.
Save tosin2013/8fad6edaae7865325e872ac1a1f0ff05 to your computer and use it in GitHub Desktop.
delete-pods.sh
#!/bin/bash
set -xe
# Get all projects in OpenShift
projects=$(oc get projects -o=jsonpath='{.items[*].metadata.name}')
# Iterate through each project
for project in $projects; do
echo "Checking project: $project"
# Get pods with non-zero restarts in the current project
oc get pods --field-selector=status.phase==Running -n $project --no-headers=true | while read line; do
pod_name=$(echo $line | awk '{print $1}')
restart_count=$(echo $line | awk '{print $4}')
if [[ $restart_count != "0" ]]; then
echo "Deleting pod: $pod_name in project: $project"
oc delete pod $pod_name -n $project --force --grace-period=0
else
echo "No pods with restarts found in project: $project"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment