Delete schema

Delete a schema resource.

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 
 ) 
  
 { 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 DeleteSchemaRequest 
  
 request 
 ; 
  
 request 
 . 
 set_name 
 ( 
 pubsub 
 :: 
 Schema 
 ( 
 project_id 
 , 
  
 schema_id 
 ). 
 FullName 
 ()); 
  
 auto 
  
 status 
  
 = 
  
 client 
 . 
 DeleteSchema 
 ( 
 request 
 ); 
  
 // Note that kNotFound is a possible result when the library retries. 
  
 if 
  
 ( 
 status 
 . 
 code 
 () 
  
 == 
  
 google 
 :: 
 cloud 
 :: 
 StatusCode 
 :: 
 kNotFound 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The schema was not found 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 if 
  
 ( 
 ! 
 status 
 . 
 ok 
 ()) 
  
 throw 
  
 std 
 :: 
 runtime_error 
 ( 
 status 
 . 
 message 
 ()); 
  
 std 
 :: 
 cout 
 << 
 "Schema successfully deleted 
 \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 
  
 DeleteSchemaSample 
 { 
  
 public 
  
 void 
  
 DeleteSchema 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 schemaId 
 ) 
  
 { 
  
  SchemaServiceClient 
 
  
 schemaService 
  
 = 
  
  SchemaServiceClient 
 
 . 
  Create 
 
 (); 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
  FromProjectSchema 
 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 schemaService 
 . 
  DeleteSchema 
 
 ( 
 schemaName 
 ); 
  
 } 
 } 
 

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" 
  
 pubsub 
  
 "cloud.google.com/go/pubsub/v2/apiv1" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
 ) 
 func 
  
 deleteSchema 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 schemaID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // schemaID := "my-schema" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewSchemaClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewSchemaClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 DeleteSchemaRequest 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 } 
  
 if 
  
 err 
  
 := 
  
 client 
 . 
 DeleteSchema 
 ( 
 ctx 
 , 
  
 req 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "client.DeleteSchema: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Deleted schema: %s" 
 , 
  
 schemaID 
 ) 
  
 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. SchemaName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 DeleteSchemaExample 
  
 { 
  
 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" 
 ; 
  
 deleteSchemaExample 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 deleteSchemaExample 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 schemaId 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 try 
  
 ( 
  SchemaServiceClient 
 
  
 schemaServiceClient 
  
 = 
  
  SchemaServiceClient 
 
 . 
 create 
 ()) 
  
 { 
  
 schemaServiceClient 
 . 
 deleteSchema 
 ( 
 schemaName 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Deleted a schema:" 
  
 + 
  
 schemaName 
 ); 
  
 } 
  
 catch 
  
 ( 
  NotFoundException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schemaName 
  
 + 
  
 "not found." 
 ); 
  
 } 
  
 } 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js 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 .

  /** 
  
 * 
  
 TODO 
 ( 
 developer 
 ): 
  
 Uncomment 
  
 this 
  
 variable 
  
 before 
  
 running 
  
 the 
  
 sample 
 . 
  
 */ 
 // 
  
 const 
  
 schemaNameOrId 
  
 = 
  
 'YOUR_SCHEMA_NAME_OR_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 
  
 deleteSchema 
 ( 
 schemaNameOrId 
 ) 
  
 { 
  
 const 
  
 schema 
  
 = 
  
 pubSubClient 
 . 
 schema 
 ( 
 schemaNameOrId 
 ); 
  
 const 
  
 name 
  
 = 
  
 await 
  
 schema 
 . 
 getName 
 (); 
  
 await 
  
 schema 
 . 
 delete 
 (); 
  
 console 
 . 
 log 
 ( 
 ` 
 Schema 
  
 $ 
 { 
 name 
 } 
  
 deleted 
 . 
 ` 
 ); 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js 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 .

  /** 
 * 
 TODO 
 ( 
 developer 
 ): 
 Uncomment 
 this 
 variable 
 before 
 running 
 the 
 sample 
 . 
 */ 
 // 
 const 
 schemaNameOrId 
 = 
 'YOUR_SCHEMA_NAME_OR_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 
 deleteSchema 
 ( 
 schemaNameOrId 
 : 
 string 
 ) 
 { 
 const 
 schema 
 = 
 pubSubClient 
 . 
 schema 
 ( 
 schemaNameOrId 
 ); 
 const 
 name 
 = 
 await 
 schema 
 . 
 getName 
 (); 
 await 
 schema 
 . 
 delete 
 (); 
 console 
 . 
 log 
 ( 
 ` 
 Schema 
 $ 
 { 
 name 
 } 
 deleted 
 . 
 ` 
 ); 
 } 
 

PHP

Before trying this sample, follow the PHP setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub PHP 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 .

  use Google\Cloud\PubSub\PubSubClient; 
 /** 
 * Delete a schema. 
 * 
 * @param string $projectId 
 * @param string $schemaId 
 */ 
 function delete_schema($projectId, $schemaId) 
 { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 $schema = $pubsub->schema($schemaId); 
 if ($schema->exists()) { 
 $schema->delete(); 
 printf('Schema %s deleted.', $schema->name()); 
 } else { 
 printf('Schema %s does not exist.', $schema->name()); 
 } 
 } 
 

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" 
 schema_client 
 = 
 SchemaServiceClient 
 () 
 schema_path 
 = 
 schema_client 
 . 
  schema_path 
 
 ( 
 project_id 
 , 
 schema_id 
 ) 
 try 
 : 
 schema_client 
 . 
 delete_schema 
 ( 
 request 
 = 
 { 
 "name" 
 : 
 schema_path 
 }) 
 print 
 ( 
 f 
 "Deleted a schema: 
 \n 
 { 
  schema_path 
 
 } 
 " 
 ) 
 except 
 NotFound 
 : 
 print 
 ( 
 f 
 " 
 { 
 schema_id 
 } 
 not found." 
 ) 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby 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 .

  # schema_id = "your-schema-id" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 schemas 
  
 = 
  
 pubsub 
 . 
 schemas 
 result 
  
 = 
  
 schemas 
 . 
 delete_schema 
  
 name 
 : 
  
 pubsub 
 . 
 schema_path 
 ( 
 schema_id 
 ) 
 puts 
  
 "Schema 
 #{ 
 schema_id 
 } 
 deleted." 
 

What's next

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

Create a Mobile Website
View Site in Mobile | Classic
Share by: