Delete service

Delete a Service Directory service.

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 DeleteServiceSample
{
    public void DeleteService(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var serviceName = ServiceName.FromProjectLocationNamespaceService(projectId, locationId, namespaceId, serviceId);
        registrationServiceClient.DeleteService(serviceName);
    }
}

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 deleteService(projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"

	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 service
	req := &sdpb.DeleteServiceRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", projectID, location, namespaceID, serviceID),
	}
	if err := client.DeleteService(ctx, req); err != nil {
		return fmt.Errorf("DeleteService: %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.RegistrationServiceClient;
import com.google.cloud.servicedirectory.v1.ServiceName;
import java.io.IOException;

public class ServicesDelete {

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

  // Delete a service.
  public static void deleteService(
      String projectId, String locationId, String namespaceId, String serviceId)
      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 service to delete.
      ServiceName serviceName = ServiceName.of(projectId, locationId, namespaceId, serviceId);

      // Send the request to delete the service.
      client.deleteService(serviceName);

      // Log the action.
      System.out.println("Deleted Service: " + serviceName.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';
// const serviceId = 'my-service';

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

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

// Build the service name
const serviceName = registrationServiceClient.servicePath(
  projectId,
  locationId,
  namespaceId,
  serviceId
);

async function deleteService() {
  await registrationServiceClient.deleteService({
    name: serviceName,
  });

  console.log(`Deleted service: ${serviceName}`);
}

deleteService();

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
 * @param string $serviceId     Your service name
 */
function delete_service(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
    $client->deleteService($serviceName);

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

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_service(
    project_id: str, location: str, namespace_id: str, service_id: str
) -> bool:
    """Deletes a service in the given namespace.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the service to delete.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    service_name = client.service_path(project_id, location, namespace_id, service_id)

    client.delete_service(name=service_name)
    print(f"Deleted service {service_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 parent namespace"
# service   = "The name of the service"

require "google/cloud/service_directory"

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

# The path of the service
service_path = registration_service.service_path(
  project:   project,
  location:  location,
  namespace: namespace,
  service:   service
)

# Use the Service Directory API to delete the service
registration_service.delete_service name: service_path
puts "Deleted service: #{service_path}"

What's next

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