Delete deployment

Delete a Game Server deployment.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Go

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.


import (
	"context"
	"fmt"
	"io"

	gaming "cloud.google.com/go/gaming/apiv1"
	gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
)

// deleteGameServerDeployment deletes a game server deployment.
func deleteGameServerDeployment(w io.Writer, projectID, deploymentID string) error {
	// projectID := "my-project"
	// deploymentID := "mydeployment"
	ctx := context.Background()
	client, err := gaming.NewGameServerDeploymentsClient(ctx)
	if err != nil {
		return fmt.Errorf("NewGameServerDeploymentsClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.DeleteGameServerDeploymentRequest{
		Name: fmt.Sprintf("projects/%s/locations/global/gameServerDeployments/%s", projectID, deploymentID),
	}

	op, err := client.DeleteGameServerDeployment(ctx, req)
	if err != nil {
		return fmt.Errorf("DeleteGameServerDeployment: %v", err)
	}
	err = op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %v", err)
	}

	fmt.Fprintf(w, "Deployment deleted.")
	return nil
}

Java

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient;
import com.google.cloud.gaming.v1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteDeployment {
  public static void deleteGameServerDeployment(String projectId, String deploymentId)
      throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // String projectId = "your-project-id";
    // String deploymentId = "your-game-server-deployment-id";
    // 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 (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) {
      String parent = String.format("projects/%s/locations/global", projectId);
      String deploymentName = String.format("%s/gameServerDeployments/%s", parent, deploymentId);

      OperationFuture<Empty, OperationMetadata> call =
          client.deleteGameServerDeploymentAsync(deploymentName);
      call.get(1, TimeUnit.MINUTES);
      System.out.println("Game Server Deployment deleted: " + deploymentName);
    }
  }
}

Node.js

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.

const {
  GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerDeploymentsServiceClient();

async function deleteGameServerDeployment() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'Your Google Cloud Project ID';
  // const deploymentId = 'A unique ID for the Game Server Deployment';
  const request = {
    // The full resource name
    name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
  };

  const [operation] = await client.deleteGameServerDeployment(request);
  await operation.promise();

  console.log(`Deployment with name ${request.name} deleted.`);
}

deleteGameServerDeployment();

Python

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.

def delete_deployment(project_id, deployment_id):
    """Deletes a game server deployment."""

    client = gaming.GameServerDeploymentsServiceClient()

    # Location is hard coded as global, as game server deployments can
    # only be created in global.  This is done for all operations on
    # game server deployments, as well as for its child resource types.
    request = game_server_deployments.DeleteGameServerDeploymentRequest(
        name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}",
    )

    operation = client.delete_game_server_deployment(request)
    print(f"Delete deployment operation: {operation.operation.name}")
    operation.result(timeout=120)