# kubectl delete 命令详解
通过配置文件名、stdin、资源名称或 label 选择器来删除资源。
支持 JSON 和 YAML 格式文件。可以只指定一种类型的参数:文件名、资源名称或 label 选择器。
有些资源,如 pod,支持优雅的(graceful)删除,因为这些资源一般是集群中的实体,所以删除不可能会立即生效,这些资源在强制终止之前默认定义了一个周期(宽限期),但是你可以使用--grace-period flag 来覆盖该值,或者通过 pass --now 设置该周期为 1。
如果托管 Pod 的 Node 节点已经停止或者无法连接 API Server,使用 delete 命令删除 Pod 需等待时间更长。要强制删除资源,需指定- force flag,且设置周期(宽限期)为 0。
如果执行强制删除 Pod,则调度程序会在节点释放这些 Pod 之前将新的 Pod 放在这些节点上,并使之前 Pod 立即被逐出。
注意:执行 delete 命令时不会检查资源版本,如果在执行 delete 操作时有人进行了更新操作,那么更新操作将连同资源一起被删除。
# 语法
$ delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])
# 示例
使用 pod.json 中指定的资源类型和名称删除 pod。
kubectl delete -f ./pod.json
根据传入 stdin 的 JSON 所指定的类型和名称删除 pod。
cat pod.json | kubectl delete -f -
删除名为“baz”和“foo”的 Pod 和 Service。
kubectl delete pod,service baz foo
删除 Label name = myLabel 的 pod 和 Service。
kubectl delete pods,services -l name=myLabel
强制删除 dead node 上的 pod
kubectl delete pod foo --grace-period=0 --force
删除所有 pod
kubectl delete pods --all
# Flags
Name | Shorthand | Default | Usage |
---|---|---|---|
all | false | select all resources in the namespace of the specified resource types. | |
cascade | true | If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true. | |
filename | f | [] | Filename, directory, or URL to files containing the resource to delete. |
force | false | Immediate deletion of some resources may result in inconsistency or data loss and requires confirmation. | |
grace-period | -1 | Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. | |
ignore-not-found | false | Treat "resource not found" as a successful delete. Defaults to "true" when --all is specified. | |
include-extended-apis | true | If true, include definitions of new APIs via calls to the API server. [default true] | |
now | false | If true, resources are signaled for immediate shutdown (same as --grace-period=1). | |
output | o | Output mode. Use "-o name" for shorter output (resource/name). | |
recursive | R | false | Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. |
selector | l | Selector (label query) to filter on. | |
timeout | 0s | The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object |