Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
Kubernetes - Kubectl Delete
Next article icon

Kubernetes - Kubectl Delete

Last Updated : 16 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later donated to CNCF (Cloud Native Computing Foundation).

Table of Content

  • What is Kubectl Delete?
    • Deleting  Deployment
    • Deleting  Service
  • Delete All Pods in All Namespaces
    • Excluding Kubernetes System Namespaces With a for Loop
  • Delete All Pods in One Namespace
  • Delete All Pods in Multiple Namespaces
  • Delete Pods Forcefully
  • Delete All Resources in All Namespaces
  • Delete All Resources in One Namespace
  • Delete All Resources in Multiple Namespaces

What is Kubectl Delete?

kubectl delete is a command used in Kubernetes to remove resources from a cluster. This command is part of the kubectl command-line tool, which is the primary interface for interacting with Kubernetes clusters. kubectl delete is used to delete resources by using a configuration file or by using the type of resource and the resource name.

kubectl delete ([-f FILENAME] | TYPE [(NAME | --all)])
  • Delete by Configuration File (-f FILENAME):
    • -f FILENAME: Specifies a configuration file containing the resource definition(s) to be deleted.
    • This mode allows you to delete resources defined in a YAML or JSON file. You provide the path to the file after the -f flag, and kubectl deletes the resources defined in that file.
  • Delete by Resource Type and Name (TYPE [(NAME | --all)]):
    • TYPE: Specifies the type of resource to be deleted (e.g., deployment, service, pod, etc.).
    • NAME: Optionally, specifies the name of the specific resource instance to be deleted.
    • --all: Deletes all instances of the specified resource type across namespaces.

Example: Suppose we are having Nginx web server deployment and Service running.

nginx file

This is the nginx service manifest file.

nginx service file

Here is the all running resources on the default namespace. Refer the below image for your reference.

kubectl get all

Deleting  Deployment

To delete the resources in Kubernetes, the kubectl delete command provides the flexibility. We can delete resources by specifying a configuration file or directly by resource type and name. For instance, to delete a Deployment named deployment_name, you execute the below command:

$ kubectl delete deployment deployment_name

Alternatively, you can also point your terminal to the file containing the deployment config file and use the command

$ kubectl delete -f your_config_file.yaml
kubectl delete command

Deleting  Service

The same principle applies to deleting Services. To delete a Service named service_name, you use:

$ kubectl delete service service_name

Alternatively, you can also point your terminal to the file containing the deployment config file and use the command

$ kubectl delete -f your_config_file.yaml
kubectl delete command

Delete All Pods in All Namespaces

Deleting Pods Across All Namespaces: Use the kubectl delete pods command with the --all flag to indicate delete the all pods in your cluster.

kubectl delete pods --all

Excluding System Pods: Since deleting system the pods might destabilize the cluster, it's prudent to exclude them. Instead of delete the pods in the kube-system namespace, explicitly delete deployments across all namespaces, except for critical system deployments.

kubectl delete deploy --all -A

Excluding Kubernetes System Namespaces With a for Loop

Suppose that you are assigned the task of managing a Kubernetes cluster's regular maintenance. The goal is to eliminate obsolete deployments and pods from different namespaces while preserving the confidentiality of the system namespaces. Let us see how you can successfully accomplish this.

Deleting Pods Across Non-System Namespaces: You can start by executing a series of commands orchestrated through a loop.

for ns in $(kubectl get namespaces -o name | grep -v kube- | cut -c 11-);
do
kubectl delete pods --all -n $ns;
done
Deleting-Pods


Here's what each step accomplishes:

  • Retrieve Namespace List: You receive a list for each namespace in the cluster through the use of kubectl.
  • Exclude System Namespaces: To be sure only those with user-defined namespaces are taken into thought, use grep to filter out any namespaces beginning with "kube-."
  • Refine Output: With cut, extra characters have been eliminated from each line ensuring the names of non-system namespaces are the only characters left.
  • Iterate and Delete: You traverse each non-system namespace in a for loop and execute kubectl delete pods --all -n $ns to eliminate every pod within each namespace.

This streamlined approach safeguards system namespaces from accidental modifications while automates the cleanup process.

Deleting Deployments Across Non-System Namespaces: In the same way, you use the same procedure to deployment removal.

for ns in $(kubectl get namespaces -o name | grep -v kube- | cut -c 11-);
do
kubectl delete deploy --all -n $ns;
done
Non-System-Namespaces

Throughout iteration through non-system namespaces, with each iteration which includes kubectl delete deploy --all -n $ns, you can swiftly eliminate outdated deployments and preserve cluster hygiene without interfering with critical system components.

Delete All Pods in One Namespace

We can use the kubectl delete command with the --all flag and the -n flag to specify the namespace so as to remove all pods within this namespace. This is the instructions:

kubectl delete pods --all -n your-namespace
All-Pods-in-One-Namespace

Put the name of the namespace from which you want to eliminate all pods in instead of your-namespace. Regardless of their status or labels, each pod in the specified namespace will be deleted with this command.

Delete All Pods in Multiple Namespaces

We can use a single kubectl command with the --all-namespaces flag and pods as the resource type to be demolished to delete all pods in all namespaces. This command speeds up the cleanup process by destroying all pods across all specified namespaces simultaneously. Before executing the command, make sure you have the appropriate permissions to avoid unanticipated results.

kubectl delete pods --all -n namespace1 -n namespace2 -n namespace3
Multiple-Namespaces

The names of the namespaces from which you wish to remove all pods ought to be substituted for namespace1, namespace2, namespace3, etc. Each pod within each namespace that has been provided is going to be deleted by this command.

Delete Pods Forcefully

Utilizing the kubectl get rid of command when combined with the --grace-period=0 parameter allows you to swiftly destroy pods in Kubernetes. With a zero-second grace time provided by this flag, here will be no waiting period until pods end gracefully. Here's how to achieve it:

kubectl delete pods --all --grace-period=0 --force
Delete-Pods-Forcefully

With this command, each pod in the current namespace will be forcefully removed. The -n contention can be utilized to denote the namespace if you want to get rid of pods from that namespace:

kubectl delete pods --all --grace-period=0 --force -n your-namespace
Delete-Pods-Forcefully

Enter the name of the namespace from which you wish to delete all pods in place of your-namespace.

Delete All Resources in All Namespaces

Removing every resource in every namespaces is an extreme step that could have significant consequences. In the case that you must move on, here's how to use kubectl to do it:

kubectl delete all --all --all-namespaces
All-Resources-in-All-Namespaces

This command will remove all resources (pods, deployments, services, etc.) in all namespaces. Make sure you completely understand the implications of continuing, as this move will affect all of the services and apps running within your Kubernetes cluster. Check you have enough privileges before running this command, and think about making backups or contacting your team to confirm.

Delete All Resources in One Namespace

To delete all resources in a specific namespace in Kubernetes, you can use the following kubectl command:

kubectl delete all --all -n your-namespace
All-Resources-in-One-Namespaces

To remove all resources, replace your-namespace with the name of the namespace. All resources (pods, deployments, services, etc.) within the specified namespace will be removed by this command.

Delete All Resources in Multiple Namespaces

To delete all resources in multiple namespaces in Kubernetes, you can execute the following command:

kubectl delete all --all -n namespace1 -n namespace2 -n namespace3
All-Resources-in-Multiple-Namespaces

The names of the namespaces from which you want to eliminate all resources must be replaced for namespace1, namespace2, namespace3, etc. All resources (pods, deployments, services, etc.) within each namespace which is given will be destroyed by this command.


Next Article
Kubernetes - Kubectl Delete

I

ishankhandelwals
Improve
Article Tags :
  • Technical Scripter
  • Kubernetes
  • DevOps
  • Technical Scripter 2022
  • Kubernetes-Basics

Similar Reads

    Kubernetes - Kubectl
    Kubectl is a command-line software tool that is used to run commands in command-line mode against the Kubernetes Cluster. Kubectl runs the commands in the command line, Behind the scenes it authenticates with the master node of Kubernetes Cluster with API calls and performs the operations on Kuberne
    12 min read
    Kubernetes - Kubectl Commands
    Pre-requisites: Kubernetes The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows:kubectl Co
    5 min read
    Kubernetes - Kubectl Create and Kubectl Apply
    Kubernetes is an open-source Container Orchestrating software platform that is widely used in modern application deployment and management. It comes up with many internal resources to manage the other working nodes Organizing as a Cluster facilitates seamless automatic scaling, and deployment update
    8 min read
    Kubernetes Cluster
    A group of nodes (Computers/Operating systems) working together to communicate with the help of Kubernetes software is known as the Kubernetes cluster. It works in a mechanism in such a way that it performs actions on the worker node with the help of the manager node. Need for a Kubernetes ClusterIn
    6 min read
    What is Kubelet in Kubernetes?
    A Kubelet in Kubernetes is a crucial component of the primary node agent that runs on each node. It assists with container management and orchestration within a Kubernetes cluster. Kubelet supports communication between the Kubernetes control plane and individual nodes and it also enables the effici
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences