Can I get or delete a pod/resource by UID?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes assigns a unique UID to every object, but kubectl does not support getting or deleting resources directly by UID. The API and CLI are designed around namespace, resource type, and name. To operate on a resource by UID, you first query for the resource whose .metadata.uid matches, then use its name. This is an extra step but ensures correctness, especially when multiple objects share similar names across namespaces.
Why kubectl Does Not Support UID Directly
Kubernetes resources are identified by the tuple (apiVersion, kind, namespace, name). UIDs are internal identifiers used for ownership references (ownerReferences), garbage collection, and conflict detection. The API server indexes resources by name, not UID, so there is no efficient single-call lookup by UID.
Get a Pod by UID
Use kubectl get with JSONPath or jq to filter by UID.
Or using kubectl with JSONPath:
Search Across All Namespaces
Delete a Pod by UID
Since kubectl delete requires a name, find the name first, then delete.
Get Any Resource by UID
The same pattern works for any Kubernetes resource type.
Using the Kubernetes API Directly
The Kubernetes API supports field selectors, but metadata.uid is not a supported field selector for most resources. You must list and filter client-side.
When UIDs Are Useful
UIDs are primarily used for:
- Owner references: Garbage collection uses UIDs to track parent-child relationships between resources.
- Conflict detection: The API server uses UIDs to ensure you are updating the correct revision of a resource.
- Audit logging: UIDs uniquely identify a specific incarnation of a resource, even if the name is reused after deletion.
Common Pitfalls
- Expecting
kubectl get pod --uid=xxxto work —kubectlhas no--uidflag. You must filter JSON output client-side. - Searching only in one namespace when the resource could be in any namespace — use
--all-namespaceswhen the namespace is unknown. - Assuming UIDs are reused after deletion — UIDs are unique within the cluster lifetime. A new pod with the same name gets a different UID.
- Using UIDs for human communication — UIDs are long and hard to read. Use resource names and namespaces for day-to-day operations, and UIDs only for programmatic correlation.
- Not handling the case where no resource matches the UID — always check for empty results before attempting delete operations.
Summary
- Kubernetes does not support direct get/delete by UID through
kubectl. - Use
kubectl get -o json | jqto filter resources by.metadata.uid, then operate by name. - UIDs are unique per resource incarnation and useful for ownership references, garbage collection, and audit trails.
- For programmatic access, list resources and filter by UID in client libraries (Python, Go, etc.).
- Always search across all namespaces when the resource location is unknown.

