Skip to content

Instantly share code, notes, and snippets.

@zonca
Last active January 28, 2026 04:08
Show Gist options
  • Select an option

  • Save zonca/cbd7add9554e7b729f350268e659ed47 to your computer and use it in GitHub Desktop.

Select an option

Save zonca/cbd7add9554e7b729f350268e659ed47 to your computer and use it in GitHub Desktop.
"#!/usr/bin/env bash\nset -euo pipefail\n\n# Target username (JupyterHub username)\nexport JH_USER=someuser\n\n# Required namespace (e.g., jhub)\n: \"${JH_NAMESPACE:?Set JH_NAMESPACE (e.g., export JH_NAMESPACE=jhub)}\"\n\n# Refuse to delete if the user's pod is still running (avoid stale NFS handles)\nif kubectl -n \"${JH_NAMESPACE}\" get pod \"jupyter-${JH_USER}\" >/dev/null 2>&1; then\n echo \"User pod jupyter-${JH_USER} is still running. Stop the server first, then re-run.\"\n exit 1\nfi\n\n# Clean up any prior job with the same name\nkubectl -n \"${JH_NAMESPACE}\" delete job \"delete-home-${JH_USER}\" --ignore-not-found\n\n# Generate Job configuration\ncat <<EOF2 > delete-user-home.yaml\napiVersion: batch/v1\nkind: Job\nmetadata:\n name: delete-home-${JH_USER}\n namespace: ${JH_NAMESPACE}\nspec:\n backoffLimit: 1\n template:\n spec:\n restartPolicy: Never\n containers:\n - name: delete-home\n image: busybox:1.36\n command:\n - /bin/sh\n - -c\n - |\n set -eu\n echo \"Deleting /mnt/${JH_USER}\"\n rm -rf \"/mnt/${JH_USER}\"\n volumeMounts:\n - name: home-nfs\n mountPath: /mnt\n volumes:\n - name: home-nfs\n persistentVolumeClaim:\n claimName: home-nfs\nEOF2\n\n# Run and cleanup the job (only delete on success)\nkubectl apply -f delete-user-home.yaml \\\n && kubectl wait --for=condition=complete job/delete-home-${JH_USER} -n ${JH_NAMESPACE} --timeout=60s \\\n && kubectl logs -n ${JH_NAMESPACE} job/delete-home-${JH_USER} \\\n && kubectl delete -n ${JH_NAMESPACE} job/delete-home-${JH_USER} \\\n || echo \"Job did not complete; leaving it in place for inspection.\"\n"
"#!/usr/bin/env bash\nset -euo pipefail\n\n# Target username (JupyterHub username)\nexport JH_USER=someuser\n\n# Required namespace (e.g., jhub)\n: \"${JH_NAMESPACE:?Set JH_NAMESPACE (e.g., export JH_NAMESPACE=jhub)}\"\n\n# Clean up any prior job with the same name\nkubectl -n \"${JH_NAMESPACE}\" delete job \"recreate-home-${JH_USER}\" --ignore-not-found\n\ncat <<EOF2 | kubectl apply -f -\napiVersion: batch/v1\nkind: Job\nmetadata:\n name: recreate-home-${JH_USER}\n namespace: ${JH_NAMESPACE}\nspec:\n backoffLimit: 1\n template:\n spec:\n restartPolicy: Never\n containers:\n - name: recreate-home\n image: busybox:1.36\n command:\n - /bin/sh\n - -c\n - |\n set -eu\n mkdir -p \"/mnt/${JH_USER}\"\n chown 1000:100 \"/mnt/${JH_USER}\"\n chmod 0770 \"/mnt/${JH_USER}\"\n volumeMounts:\n - name: home-nfs\n mountPath: /mnt\n volumes:\n - name: home-nfs\n persistentVolumeClaim:\n claimName: home-nfs\nEOF2\n\nkubectl wait --for=condition=complete job/recreate-home-${JH_USER} -n ${JH_NAMESPACE} --timeout=60s \\\n && kubectl logs -n ${JH_NAMESPACE} job/recreate-home-${JH_USER} \\\n && kubectl delete -n ${JH_NAMESPACE} job/recreate-home-${JH_USER} \\\n || echo \"Job did not complete; leaving it in place for inspection.\"\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment