Delete a revision from a schema

Delete a revision from a schema

Explore further

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

Code sample

C++

Before trying this sample, follow the C++ setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub C++ API reference documentation.

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

namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
   std::string const& schema_id, std::string const& revision_id) {
  std::string const schema_id_with_revision = schema_id + "@" + revision_id;

  google::pubsub::v1::DeleteSchemaRevisionRequest request;
  request.set_name(
      pubsub::Schema(project_id, schema_id_with_revision).FullName());
  auto schema = client.DeleteSchemaRevision(request);
  if (!schema) throw std::move(schema).status();

  std::cout << "Deleted schema. Its metadata is:"
            << "\n"
            << schema->DebugString() << "\n";
}

C#

Before trying this sample, follow the C# setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub C# API reference documentation.

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


using Google.Cloud.PubSub.V1;

public class DeleteSchemaRevisionSample
{
    public void DeleteSchemaRevision(string projectId, string schemaId, string revisionId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        DeleteSchemaRevisionRequest request = new DeleteSchemaRevisionRequest
        {
            SchemaName = SchemaName.FromProjectSchema(projectId, schemaId + "@" + revisionId)
        };
        schemaService.DeleteSchemaRevision(request);
    }
}

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Go API reference documentation.

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

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsub"
)

func deleteSchemaRevision(w io.Writer, projectID, schemaID, revisionID string) error {
	// projectID := "my-project-id"
	// schemaID := "my-schema-id"
	// revisionID := "my-revision-id"
	ctx := context.Background()
	client, err := pubsub.NewSchemaClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
	}
	defer client.Close()

	if _, err := client.DeleteSchemaRevision(ctx, schemaID, revisionID); err != nil {
		return fmt.Errorf("client.DeleteSchema revision: %w", err)
	}
	fmt.Fprintf(w, "Deleted a schema revision: %s@%s", schemaID, revisionID)
	return nil
}

Java

Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Java API reference documentation.

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


import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.DeleteSchemaRevisionRequest;
import com.google.pubsub.v1.SchemaName;
import java.io.IOException;

public class DeleteSchemaRevisionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String schemaId = "your-schema-id@your-revision-id";

    deleteSchemaRevisionExample(projectId, schemaId);
  }

  public static void deleteSchemaRevisionExample(String projectId, String schemaId)
      throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);

    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {

      DeleteSchemaRevisionRequest request =
          DeleteSchemaRevisionRequest.newBuilder().setName(schemaName.toString()).build();

      schemaServiceClient.deleteSchemaRevision(request);

      System.out.println("Deleted a schema revision:" + schemaName);

    } catch (NotFoundException e) {
      System.out.println(schemaName + "not found.");
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function deleteSchemaRevision(schemaNameOrId, revisionId) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

  console.log(`Schema ${name} revision ${revisionId} deleted.`);
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
import {PubSub} from '@google-cloud/pubsub';

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function deleteSchemaRevision(
  schemaNameOrId: string,
  revisionId: string
) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

  console.log(`Schema ${name} revision ${revisionId} deleted.`);
}

Python

Before trying this sample, follow the Python setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Python API reference documentation.

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

from google.api_core.exceptions import NotFound
from google.cloud.pubsub import SchemaServiceClient

# TODO(developer): Replace these variables before running the sample.
# project_id = "your-project-id"
# schema_id = "your-schema-id"
# revision_id = "your-revision-id"

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id + "@" + revision_id)

try:
    schema_client.delete_schema_revision(request={"name": schema_path})
    print(f"Deleted a schema revision:\n{schema_path}")
except NotFound:
    print(f"{schema_id} not found.")

What's next

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