
This assumes you have used the REST API to obtain an OAuth bearer token. Let's say the bearer token is sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U.
Before deleting a secret, you will probably want to list the secrets in a namespace to ensure the secret you want to delete exists. Here is how you would list the secrets in the "default" namespace using the curl REST API.
curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.lab001.op.example.com:6443/api/v1/namespaces/default/secrets"
If the items array contains key value pairs, this means the namespace contains one or more secrets.
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "mysecret",
"namespace": "default",
"selfLink": "/api/v1/namespaces/default/secrets/mysecret",
"uid": "cf858bde-be0f-40ae-a882-2daa815335a4",
"resourceVersion": "136899647",
"creationTimestamp": "2021-11-18T11:51:46Z",
"managedFields": [
{
"manager": "kubectl-create",
"operation": "Update",
"apiVersion": "v1",
"time": "2021-11-18T11:51:46Z",
"fieldsType": "FieldsV1",
"fieldsV1": {"f:data":{".":{},"f:foo":{}},"f:type":{}}
}
]
},
"data": {
"foo": "YmFy"
},
"type": "Opaque"
}
Or to return a specific secret, such as "mysecret".
curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.lab001.op.example.com:6443/api/v1/namespaces/default/secrets/mysecret"
Here is how you would delete "mysecret".
curl
--insecure
--request DELETE
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.lab001.op.example.com:6443/api/v1/namespaces/default/secrets/mysecret"
If the delete is successful, something like this should be returned.
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Success",
"details": {
"name": "mysecret",
"kind": "secrets",
"uid": "49fadd8b-bc73-4880-ae83-e4c8fed726b9"
}
}
If the delete fails, such as when the secret does not exist, something like this should be returned.
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "secrets \"bogus\" not found",
"reason": "NotFound",
"details": {
"name": "bogus",
"kind": "secrets"
},
"code": 404
}
Did you find this article helpful?
If so, consider buying me a coffee over at