Skip to the content
Fabian Lee : Software EngineerFabian Lee : Software Engineer
Cloud Operations and Development
  • Monitoring
  • Logging
  • Containers
  • Python

Kubernetes: alternative to export for removing internal fields from yaml

January 26, 2022
Categories: Linux

The ‘export’ flag in kubectl was deprecated in 1.18, but you still need a way to dump the yaml of Kubernetes objects, often for the purpose of reapplying changes.

The problem with doing a simple ‘get’ using a yaml output like below is that additional internal accounting attributes like ‘selfLink’, ‘creationTimestamp’, ‘uid’, will be present in the capture which makes it non-portable and breaks reapply.

# captures all elements and attributes, even internal accounting
kubectl get pod <podname> -n <namespace> -o=yaml > my-object.yaml

There are utilities such as kubectl-neat that can be used, but you can also use the jq utility to remove these elements.  Below is the jq command to delete the elements, then yq to convert back to yaml.

kubectl get pod <podname> -n <namespace> -o=json | jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences)' | yq eval - -P > my-object.cleaned.yaml

This would make it easy to reapply the yaml, or apply in a different cluster.

REFERENCES

stackoverflow, stripping yaml elements with jq

https://github.com/kubernetes/kubernetes/issues/50454

kubectl export is deprecated

kubectl-neat plugin

reddit.com, kubectl export deprecated in 1.18

NOTES

older yq versions do not have eval or ‘P’ flag for pretty print of yaml

# newer versions from mikefarah
yq eval - -P

# older versions
yq read -

 

Categories: Linux Tags: clean, deprecated, export, kubectl, metadata, remove, strip, yaml

Post navigation

← Kubernetes: jsonpath range to iterate list and extract fields
Kubernetes: using kubectl to wait for condition of pods, deployments, services →
© 2025 Fabian Lee : Software Engineer