Delete namespace

Delete a Service Directory namespace.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


using Google.Cloud.ServiceDirectory.V1;

public class DeleteNamespaceSample
{
    public void DeleteNamespace(
        string projectId = "projectId",
        string locationId = "us-east1",
        string namespaceId = "test-namespace")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var namespaceName = NamespaceName.FromProjectLocationNamespace(projectId, locationId, namespaceId);
        registrationServiceClient.DeleteNamespace(namespaceName);
    }
}

Go

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import (
	"context"
	"fmt"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func deleteNamespace(projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Delete a Namespace.
	req := &sdpb.DeleteNamespaceRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", projectID, location, namespaceID),
	}
	if err := client.DeleteNamespace(ctx, req); err != nil {
		return fmt.Errorf("DeleteNamespace: %w", err)
	}
	return nil
}

Java

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import com.google.cloud.servicedirectory.v1.NamespaceName;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import java.io.IOException;

public class NamespacesDelete {

  public static void deleteNamespace() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory namespace.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    deleteNamespace(projectId, locationId, namespaceId);
  }

  // Delete a namespace.
  public static void deleteNamespace(String projectId, String locationId, String namespaceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The namespace to delete.
      NamespaceName namespaceName = NamespaceName.of(projectId, locationId, namespaceId);

      // Send the request to delete the namespace.
      client.deleteNamespace(namespaceName);

      // Log the action.
      System.out.println("Deleted Namespace: " + namespaceName.toString());
    }
  }
}

Node.js

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the namespace name
const namespaceName = registrationServiceClient.namespacePath(
  projectId,
  locationId,
  namespaceId
);

async function deleteNamespace() {
  await registrationServiceClient.deleteNamespace({
    name: namespaceName,
  });

  console.log(`Deleted namespace: ${namespaceName}`);
}

deleteNamespace();

PHP

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 */
function delete_namespace(
    string $projectId,
    string $locationId,
    string $namespaceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
    $client->deleteNamespace($namespaceName);

    // Print results.
    printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName);
}

Python

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

def delete_namespace(project_id: str, location: str, namespace_id: str) -> bool:
    """Deletes a namespace in the given location.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace to delete.
        namespace_id: The id for the namespace to delete.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    namespace_name = client.namespace_path(project_id, location, namespace_id)

    client.delete_namespace(name=namespace_name)
    print(f"Deleted namespace {namespace_name}.")
    return True

Ruby

To learn how to install and use the client library for Service Directory, see Service Directory client libraries.

To authenticate to Service Directory, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the namespace"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The path of the namespace
namespace_name = registration_service.namespace_path(
  project: project, location: location, namespace: namespace
)

# Use the Service Directory API to delete the namespace
registration_service.delete_namespace name: namespace_name
puts "Deleted namespace: #{namespace_name}"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.