Client-Side Caching in kubectl
kubectl uses several caching mechanisms to reduce redundant API calls and improve performance. Understanding these caches helps you troubleshoot unexpected behavior and optimize workflows.
Discovery Cache
kubectl caches API resource discovery information so it does not need to query the API server for available resources on every command:
# Discovery cache location
ls ~/.kube/cache/discovery/
# Each cluster has its own cache directory
ls ~/.kube/cache/discovery/my-cluster_6443/
# Force refresh the discovery cache
kubectl api-resources --cached=false
# Clear the cache manually
rm -rf ~/.kube/cache/discovery/
The discovery cache refreshes automatically every 10 minutes. If you install a CRD and kubectl does not recognize it immediately, clearing this cache resolves the issue.
HTTP Cache
kubectl caches HTTP responses using standard ETags and cache-control headers:
# HTTP cache location
ls ~/.kube/cache/http/
# Clear HTTP cache
rm -rf ~/.kube/cache/http/
# View the full cache directory
du -sh ~/.kube/cache/
Cache Invalidation
# If kubectl shows stale data, clear all caches
rm -rf ~/.kube/cache/
# Verify a fresh request is made
kubectl get pods -v=6 2>&1 | grep "GET"
# Use verbosity to see cache behavior
kubectl get nodes -v=7 2>&1 | grep -i cache
Optimizing Cache Usage
# Use --cached flag with api-resources for speed
kubectl api-resources --cached
# Reduce calls by specifying API group directly
kubectl get deployments.apps myapp
# Use server-side printing to reduce data transfer
kubectl get pods --server-print=true
The cache directory typically uses only a few megabytes of disk. Avoid deleting it routinely since it significantly speeds up repeated commands, especially in clusters with many CRDs.