How to Delete Kubernetes Resources

Published: August 21, 2020 by Author's Photo Shane Rainville | Reading time: 2 minutes
Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

What’s Covered

  • Learn how to delete Kubernetes resources using manifest files.
  • Learn how to delete Kubernetes resources without manifest files.

Deleting Resource using Manifests

The simplest method of deleting any resource in Kubernetes is to use the specific manifest file used to create it. With the manifest file on hand we can use the kubectl delete command with the -f flag.

The manifest file contains all of the information to target a specifc resource. We do not need to specify any other information, such as namespace or label.

Syntax

kubectl delete -f <path/to/file>

Example

kubectl delete -f namespace.yaml

Deleting Resources

A manifest file is not required for deleting resources. Instead, the resource can be targetted directly using the kubectl delete command. This method is more effective when targeting a group of resources or for deleting all resources in the cluster or a namespace.

syntax

kubectl delete <type> <name> [-n <namespace>] | --all | -l <label>]

Dry Runs

Deleting resources can be a risky operation. When using complex matches for deleting resources it is a good practice to perform a dryrun of the deletion. This will allow you to preview exactly what Kuberentes will delete, ensuring you do not accidently delete something in error.

kubectl delete ns --all --dry-run

Deleting All Resources

Current Namespace

To do a mass delete of all resources in your current namespace context, you can execute the kubectl delete command with the -all flag.

kubectl delete --all

Specific Namespace

To delete all resources from a specific namespace us the -n flag.

kubectl delete -n wordpress --all

All Namespaces

To delete all resources from all namespaces we can use the -A flag.

kubectl delete -A

Deleting Resource Types

Deleting Namespaces

Syntax

kubectl delete ns <name>

Example

kubectl delete ns myapp

Deleting Pods

Syntax

kubectl delete pod <name> [-n <namespace>]

Example #1

kubectl delete pod worker-cgxxv

To delete a pod located in a different namespace you must use the -n flag with the namespace’s name. Example #2

kubectl delete pod worker-cgxxv -n myapp

Deleting Services

Syntax

kubectl delete svc <name>

Example 1

kibectl delete svc nginx

Example 2

kubectl delete svc nginx -n wordpress

Deleting Deployments

kubectl delete deployment <name>
Author Photo
Blogger, Developer, pipeline builder, cloud engineer, and DevSecOps specialist. I have been working in the cloud for over a decade and running containized workloads since 2012, with gigs at small startups to large financial enterprises.

How to Update Kubernetes Deployments

Publised September 11, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Restarting Kubernetes Pods

Publised August 24, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Effectively use Kubernetes Quality of Service

Publised October 6, 2021 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Deploy Jekyll on Kubernetes

Publised September 15, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Configure Node-based apps in Kubernetes

Publised September 9, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Backup and Restore MongoDB Deployment on Kubernetes

Publised September 3, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Immediately Start Kubernetes CronJobs Manually

Publised September 2, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.

How to Copy Files to a Pod Container in Kubernetes

Publised August 27, 2020 by Shane Rainville

Learn how to delete Kubernetes resources by using a manifest file or by targeting the resource directly through the command-line.