<p>I'm trying to delete resources of a particular kind in a k8s cluster using client-go.</p>
<p>I'm using this code but it requires a specific namespace to be declared, but i want to delete this resource in all namespaces.</p>
<pre><code>    u.SetName(&quot;test&quot;)
    u.SetNamespace(v1.NamespaceAll)
    u.SetGroupVersionKind(schema.GroupVersionKind{
        Group:   &quot;group&quot;,
        Kind:    &quot;kind&quot;,
        Version: &quot;v1&quot;,
    })
    err := k8sClient.Delete(context.TODO(), u)

    if err != nil {
        fmt.Println(err.Error())
        return err
    }
</code></pre>
<p>Found the example here - <a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client" rel="noreferrer">https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client</a>
but it doesn't mention anything about all namespaces.
Could someone plz provide a way to figure this out.</p>
<p>NOTE:
This is custom resource. not default kind such as pod or deployment etc</p>

aditya
<p>Use the <code>List</code> method to get a list of all resources in <code>all namespaces</code> and then loop through the list and delete each resource using the <code>Delete</code> method.</p>
<pre class="lang-js prettyprint-override"><code>cr := &amp;v1alpha1.CustomResource{}

// Get a list of all instances of your custom resource in all namespaces
listOpts := []client.ListOption{
    client.InNamespace(v1.NamespaceAll),
}

err := k8sClient.List(context.Background(), cr, listOpts...)
if err != nil {
    return err
}

// Loop through the list and delete each instance of your custom resource
for _, item := range cr.Items {
    err = k8sClient.Delete(context.Background(), &amp;item)
    if err != nil {
        return err
    }
}
</code></pre>

