kubectl delete

Synopsis

透過檔名、標準輸入、資源和名稱,或資源和標籤選擇器刪除資源。

接受 JSON 和 YAML 格式。只能指定一種型別的引數:檔名、資源和名稱,或資源和標籤選擇器。

某些資源(如 pod)支援優雅刪除。這些資源定義了一個在被強制終止之前的預設時間段(寬限期),但您可以使用 `--grace-period` 標誌覆蓋該值,或者傳遞 `--now` 將寬限期設定為 1。由於這些資源通常代表叢集中的實體,刪除可能不會立即得到確認。如果託管 pod 的節點出現故障或無法訪問 API 伺服器,終止可能需要比寬限期長得多的時間。要強制刪除資源,您必須指定 `--force` 標誌。注意:只有部分資源支援優雅刪除。如果不支援,將忽略 `--grace-period` 標誌。

重要提示:強制刪除 pod 不會等待 pod 程序已終止的確認,這可能導致這些程序一直執行,直到節點檢測到刪除並完成優雅刪除。如果您的程序使用共享儲存或與遠端 API 通訊,並依賴 pod 名稱來識別自身,強制刪除這些 pod 可能會導致不同機器上執行的多個程序使用相同的標識,從而可能導致資料損壞或不一致。僅當您確定 pod 已終止,或者您的應用程式可以容忍同一 pod 的多個副本同時執行時,才應強制刪除 pod。此外,如果您強制刪除 pod,排程程式可能會在節點釋放這些資源之前將新 pod 放置在這些節點上,從而導致這些 pod 被立即驅逐。

請注意,delete 命令**不**執行資源版本檢查,因此如果您提交刪除的同時有人提交了對資源的更新,他們的更新將與資源的其餘部分一起丟失。

在刪除 CustomResourceDefinition 後,發現快取的無效化可能需要長達 6 小時。如果您不想等待,可以執行“kubectl api-resources”來刷新發現快取。

kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)])

示例

  # Delete a pod using the type and name specified in pod.json
  kubectl delete -f ./pod.json
  
  # Delete resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl delete -k dir
  
  # Delete resources from all files that end with '.json'
  kubectl delete -f '*.json'
  
  # Delete a pod based on the type and name in the JSON passed into stdin
  cat pod.json | kubectl delete -f -
  
  # Delete pods and services with same names "baz" and "foo"
  kubectl delete pod,service baz foo
  
  # Delete pods and services with label name=myLabel
  kubectl delete pods,services -l name=myLabel
  
  # Delete a pod with minimal delay
  kubectl delete pod foo --now
  
  # Force delete a pod on a dead node
  kubectl delete pod foo --force
  
  # Delete all pods
  kubectl delete pods --all
  
  # Delete all pods only if the user confirms the deletion
  kubectl delete pods --all --interactive

Options

--all

刪除指定資源型別名稱空間中的所有資源。

-A, --all-namespaces

如果存在,則列出所有名稱空間中的請求物件。即使指定了 --namespace,也會忽略當前上下文中的名稱空間。

--cascade string[="background"]     預設值: "background"

必須是“background”、“orphan”或“foreground”。選擇依賴項(例如,由 ReplicationController 建立的 Pod)的刪除級聯策略。預設為 background。

--dry-run string[="unchanged"]     預設值: "none"

必須是 "none"、"server" 或 "client"。如果是 client 策略,則僅列印將要傳送的物件,而不傳送它。如果是 server 策略,則提交伺服器端請求而不持久化資源。

--field-selector string

用於過濾的選擇器(欄位查詢),支援 '=', '==', 和 '!='。(例如 --field-selector key1=value1,key2=value2)。伺服器僅支援每種型別的有限數量的欄位查詢。

-f, --filename strings

包含要刪除的資源。

--force

如果為 true,則立即從 API 中刪除資源並繞過正常刪除。請注意,立即刪除某些資源可能導致不一致或資料丟失,並需要確認。

--grace-period int     預設值: -1

在資源終止之前給予的優雅終止時間(秒)。如果為負數則忽略。設定為 1 以立即關閉。僅當 --force 為 true(強制刪除)時才能設定為 0。

-h, --help

刪除的幫助

--ignore-not-found

將“資源未找到”視為成功刪除。當指定 `--all` 時,預設為“true”。

-i, --interactive

如果為 true,則在使用者確認後才刪除資源。

-k, --kustomize string

處理 kustomization 目錄。此標誌不能與 -f 或 -R 一起使用。

--now

如果為 true,則向資源發出立即關機訊號(與 `--grace-period=1` 相同)。

-o, --output string

輸出模式。使用“-o name”以獲得更短的輸出(資源/名稱)。

--raw string

直接傳送到伺服器的原始 URI,用於 DELETE 操作。使用 kubeconfig 檔案指定的傳輸。

-R, --recursive

遞迴處理 -f, --filename 指定的目錄。當您想管理組織在同一目錄下的相關清單時很有用。

-l, --selector string

用於過濾的選擇器(標籤查詢),支援 '=', '==', '!=', 'in', 'notin'。(例如 -l key1=value1,key2=value2,key3 in (value3))。匹配的物件必須滿足所有指定的標籤約束。

--timeout duration

放棄刪除之前等待的時間長度,零表示根據物件大小確定超時

--wait     預設值:true

如果為 true,則在返回之前等待資源消失。這將等待 finalizers。

繼承的父選項

--as string

為操作假定的使用者名稱。使用者可以是常規使用者或名稱空間中的服務帳戶。

--as-group strings

為操作假定的組,此標誌可以重複以指定多個組。

--as-uid string

為操作假定的 UID。

--cache-dir string     預設值: "$HOME/.kube/cache"

預設快取目錄

--certificate-authority string

證書頒發機構的證書檔案路徑

--client-certificate string

TLS 的客戶端證書檔案路徑

--client-key string

TLS 的客戶端金鑰檔案路徑

--cluster string

要使用的 kubeconfig 叢集名稱

--context string

要使用的 kubeconfig 上下文名稱

--disable-compression

如果為 true,則選擇退出所有到伺服器的請求的響應壓縮

--insecure-skip-tls-verify

如果為 true,則不會檢查伺服器證書的有效性。這將使您的 HTTPS 連線不安全。

--kubeconfig string

用於 CLI 請求的 kubeconfig 檔案路徑。

--kuberc string

用於首選項的 kuberc 檔案路徑。可以透過匯出 KUBECTL_KUBERC=false 功能標誌或關閉 KUBERC=off 功能來停用此功能。

--match-server-version

要求伺服器版本與客戶端版本匹配

-n, --namespace string

如果存在,則為本次 CLI 請求的名稱空間範圍

--password string

API 伺服器基本身份驗證的密碼

--profile string     預設值: "none"

要捕獲的配置檔名稱。選擇之一(none|cpu|heap|goroutine|threadcreate|block|mutex)

--profile-output string     預設值: "profile.pprof"

要將配置檔案寫入的檔案的名稱

--request-timeout string     預設值: "0"

在放棄單個伺服器請求之前等待的時間。非零值應包含相應的時間單位(例如 1s、2m、3h)。零值表示不超時請求。

-s, --server string

Kubernetes API 伺服器的地址和埠

--storage-driver-buffer-duration duration     預設值: 1m0s

儲存驅動程式中的寫入將緩衝此持續時間,並作為單個事務提交到非記憶體後端。

--storage-driver-db string     預設值: "cadvisor"

資料庫名稱

--storage-driver-host string     預設值: "localhost:8086"

資料庫主機:埠

--storage-driver-password string     預設值: "root"

資料庫密碼

--storage-driver-secure

使用安全的資料庫連線

--storage-driver-table string     預設值: "stats"

表名

--storage-driver-user string     預設值: "root"

資料庫使用者名稱

--tls-server-name string

用於伺服器證書驗證的伺服器名稱。如果未提供,則使用用於聯絡伺服器的主機名。

--token string

API 伺服器身份驗證的 Bearer token

--user string

要使用的 kubeconfig 使用者名稱

--username string

API 伺服器基本身份驗證的使用者名稱

--version version[=true]

--version, --version=raw 列印版本資訊並退出;--version=vX.Y.Z... 設定報告的版本

--warnings-as-errors

將從伺服器收到的警告視為錯誤,並以非零退出程式碼退出。

另請參閱

  • kubectl - kubectl 控制 Kubernetes 叢集管理器

本頁面是自動生成的。

如果你打算報告此頁面存在的問題,請在問題描述中提及此頁面是自動生成的。修復可能需要在 Kubernetes 專案的其他地方進行。

最後修改於 2025 年 9 月 4 日下午 3:30 PST: Update kubectl reference for v1.34 (bdc4bba2a5)