How to delete networkpolicies using kubectl?

8/17/2017

I've followed a Kubernetes tutorial similar to: https://kubernetes.io/docs/tasks/administer-cluster/declare-network-policy/ which created some basic networkpolicies as follows:

root@server:~# kubectl get netpol -n policy-demo
NAME           POD-SELECTOR   AGE
access-nginx   run=nginx      49m
default-deny   <none>         50m

I saw that I can delete the entire namespace (pods included) using a command like "kubectl delete ns policy-demo", but I can't see what command I need to use if I just want to delete a single policy (or edit it even).

How would I use kubectl to delete just the "access-nginx" policy above?

-- DaveUK
kubectl
kubernetes
nginx

2 Answers

9/14/2019
  • PolicyName: access-nginx
  • Namespace: default
kubectl get netpol -n default
NAME           POD-SELECTOR   AGE
access-nginx   run=nginx      60m

  • Deleting Network Policy
kubectl delete networkpolicy access-nginx -n default

or

kubectl delete netpol access-nginx -n default

or - using the filename of the resource

kubectl delete -f access-nginx.yaml

  • Editing Networking Policy

By default it will open in yaml format for editing

kubectl edit netpol access-nginx -n default

or - if you prefer in json format

kubectl edit netpol access-nginx -n default -o json

or - using the filename of the resource

kubectl edit -f access-nginx.yaml

Editor preference:

KUBE_EDITOR='nano' kubectl edit netpol access-nginx -n default

or - vim

KUBE_EDITOR='vim' kubectl edit netpol access-nginx -n default

In case of default namespace it is not required to pass -n default as kubectl considers default namespace as default.

-- Tanveer Alam
Source: StackOverflow

8/17/2017

This should work. A similar command works at my end.

kubectl -n policy-demo delete networkpolicy access-nginx
-- Eugene Chow
Source: StackOverflow