Kubectl can cache Kubernetes API discovery information and HTTP responses on your computer. If that information becomes stale, commands such as kubectl api-resources or commands that use a recently added API resource may return confusing results. Clearing the local kubectl cache is usually safe and quick—but it is important to remove the right files. The cache is separate from your Kubernetes configuration, credentials, and cluster data.
This guide explains how to clear the kubectl cache on macOS, Linux, and Windows, how to handle a custom cache location, and what to check if clearing the cache does not solve the problem.
What Does the Kubectl Cache Store?
Kubectl uses a local cache to avoid repeatedly asking the Kubernetes API server for information. In particular, it caches API discovery data: details about the API groups, versions, and resource types available in a cluster. It may also store HTTP cache data used during requests.
On macOS and Linux, the default cache directory is generally ~/.kube/cache. On Windows, it is generally under your user profile at %USERPROFILE%\.kube\cache. The exact location can differ if you use a custom cache directory or a different home directory.
The cache is not the same as your kubeconfig file. Your kubeconfig—often located at ~/.kube/config—contains cluster, user, and context settings. Removing the cache does not delete Kubernetes resources from your cluster, change your current context, or remove your kubeconfig. Avoid deleting your entire .kube directory unless you intend to remove those configuration files too.
When Should You Clear the Kubectl Cache?
Clearing the cache can help when kubectl appears to have outdated information about the API server. For example, a cluster administrator may have installed a new CustomResourceDefinition (CRD), enabled an API, or upgraded the cluster, but your local client may still be using cached discovery data.
Symptoms can include a newly available resource not appearing in kubectl api-resources, an error saying that the server does not recognize a resource type, or unexpected behavior after changing clusters or upgrading Kubernetes. These symptoms can also have other causes, such as using the wrong context, lacking permission, or having a CRD that has not finished becoming established. Treat cache clearing as a useful troubleshooting step, not a fix for every kubectl error.
Clear the Kubectl Cache on macOS or Linux
Close any scripts or terminal processes that are actively running kubectl commands. Then remove the discovery and HTTP cache directories from the default location:
rm -rf "$HOME/.kube/cache/discovery" "$HOME/.kube/http-cache"
The command removes only those two cache directories. If one does not exist, that is usually fine. You can check the remaining cache contents with:
ls -la "$HOME/.kube/cache"
If you want to remove the entire default kubectl cache rather than just the common subdirectories, use:
rm -rf "$HOME/.kube/cache"
Removing the whole cache is generally safe, but kubectl will need to recreate cached information the next time it makes relevant API requests. The targeted command is a sensible first choice because it avoids deleting unrelated files that another tool may have placed in the cache directory.
Clear the Kubectl Cache on Windows
In PowerShell, remove the default discovery and HTTP cache directories with:
Remove-Item -Recurse -Force "$HOME\.kube\cache\discovery" -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force "$HOME\.kube\http-cache" -ErrorAction SilentlyContinue
The -ErrorAction SilentlyContinue option prevents PowerShell from displaying an error if a directory is missing. To remove the whole default cache instead, run:
Remove-Item -Recurse -Force "$HOME\.kube\cache" -ErrorAction SilentlyContinue
PowerShell’s $HOME variable points to your user profile directory. If you are using Command Prompt rather than PowerShell, you can remove the cache folder with:
rmdir /s /q "%USERPROFILE%\.kube\cache"
Be careful to enter the path exactly. Do not replace cache with .kube unless you mean to remove your Kubernetes configuration as well.
Clear a Cache in a Custom Location
Kubectl supports a --cache-dir option, so your cache may not be in the default location. If you or a wrapper script supplies this option, clear the cache directory specified there rather than assuming it is under ~/.kube.
Check the command you use to run kubectl, including aliases, shell scripts, and automation. You can also review kubectl’s available options with:
kubectl options
Look for the --cache-dir option and its default value. If you use a custom directory, remove the relevant discovery and http-cache subdirectories inside that location. For example, if the cache directory is /tmp/kubectl-cache, the targeted cleanup would be:
rm -rf "/tmp/kubectl-cache/discovery" "/tmp/kubectl-cache/http-cache"
Use the equivalent path and removal command for your operating system. If you are unsure which directory a tool or script uses, confirm the setting before deleting files.
Refresh API Discovery After Clearing the Cache
Once the cache is removed, run the command that was failing. Kubectl will request fresh discovery information and rebuild the cache as needed. To check the resources the current cluster advertises, run:
kubectl api-resources
You can also confirm which cluster and namespace your commands are using:
kubectl config current-context
kubectl config view --minify
If you are troubleshooting a particular resource, retry the original command after refreshing discovery. For a CRD, an administrator can check whether the definition is present and established with commands such as:
kubectl get crd
kubectl get crd <crd-name> -o yaml
Replace <crd-name> with the actual CRD name. Clearing your local cache cannot make a missing or not-yet-established CRD appear on the cluster; it only makes kubectl request current API information again.
Does Kubectl Have a Cache-Clearing Command?
In typical kubectl installations, there is no general-purpose kubectl cache clear command. The standard approach is to remove the relevant local cache directory yourself and let kubectl rebuild it. Be cautious with third-party plugins or scripts that offer cache-management commands: they are not necessarily part of kubectl, and their behavior can vary.
You usually do not need to reinstall kubectl, delete your kubeconfig, or restart the Kubernetes cluster to clear the local cache. Restarting the API server is not an appropriate way to fix a cache stored on your own computer.
What Clearing the Cache Will Not Fix
The kubectl cache is only one part of the connection between your terminal and a Kubernetes cluster. If the same error persists after clearing it, check the underlying issue rather than repeatedly deleting files.
-
Wrong cluster or context: Use
kubectl config current-contextto confirm that kubectl is connected to the intended cluster. - Authentication or authorization errors: Check your login method, credentials, and permissions. Removing the discovery cache does not renew credentials or grant access.
- Network or API server problems: Confirm that the cluster is reachable and that the API server is healthy.
- Missing resources or CRDs: Verify that the resource is installed and available on the cluster, and that a new CRD has finished becoming established.
- External credential caches: Cloud login tools and authentication plugins may maintain their own token caches outside kubectl’s cache directory. Follow the provider’s guidance for those tools.
Also distinguish kubectl’s cache from other caches on your computer. For example, deleting ~/.kube/cache will not clear a container image cache, a cloud provider’s credential cache, or a proxy’s cache. Identify which component is producing the stale information before removing additional files.
Quick Summary
To clear the default kubectl cache on macOS or Linux, remove ~/.kube/cache/discovery and ~/.kube/http-cache. On Windows, remove the corresponding directories under %USERPROFILE%\.kube\cache. If kubectl uses a custom --cache-dir, clean the cache at that location instead.
Afterward, run the failing command or kubectl api-resources to make kubectl fetch fresh discovery information. Keep your kubeconfig and credentials intact, and remember that cache clearing cannot fix a wrong context, missing permissions, an unreachable API server, or a resource that does not exist on the cluster.
Conclusion
Clearing the kubectl cache is a straightforward troubleshooting step when API discovery information seems stale. Remove the relevant cache folders—not your entire Kubernetes configuration—then retry your command so kubectl can rebuild its local data. If the problem remains, verify your context, access, cluster health, and resource availability to find the real cause.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.