Delete asset (v1)

Delete an asset resource.

Code sample

Go

To learn how to install and use the client library for Live Stream API, see Live Stream API client libraries. For more information, see the Live Stream API Go API reference documentation.

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


import (
	"context"
	"fmt"
	"io"

	livestream "cloud.google.com/go/video/livestream/apiv1"
	livestreampb "cloud.google.com/go/video/livestream/apiv1/livestreampb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// deleteAsset deletes an asset resource.
func deleteAsset(ctx context.Context, w io.Writer, projectID string, locationID string, assetID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/assets/%s", projectID, locationID, assetID)

	client, err := livestream.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("livestream.NewClient: %w", err)
	}
	defer client.Close()

	req := &livestreampb.DeleteAssetRequest{
		Name: name,
	}

	op, err := client.DeleteAsset(ctx, req)
	if err != nil {
		if status.Code(err) == codes.NotFound {
			fmt.Fprintf(w, "Asset not found: %s\n", name)
			return nil
		}
		return fmt.Errorf("client.DeleteAsset: %w", err)
	}

	if err := op.Wait(ctx); err != nil {
		if status.Code(err) == codes.NotFound {
			fmt.Fprintf(w, "Asset not found: %s\n", name)
			return nil
		}
		return fmt.Errorf("op.Wait: %w", err)
	}

	fmt.Fprintf(w, "Deleted asset: %s\n", name)

	return nil
}

Node.js

To learn how to install and use the client library for Live Stream API, see Live Stream API client libraries. For more information, see the Live Stream API Node.js API reference documentation.

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

'use strict';

const {LivestreamServiceClient} = require('@google-cloud/livestream');
const {status} = require('@grpc/grpc-js');

const client = new LivestreamServiceClient();

/**
 * Deletes an asset.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 * @param {string} assetId - The asset identifier (for example, 'example-asset-id')
 */
async function deleteAsset(projectId, locationId, assetId) {
  let project = projectId;
  let location = locationId;
  let asset = assetId;

  // Handle destructured object argument
  if (typeof projectId === 'object' && projectId !== null) {
    project = projectId.projectId || projectId.project_id;
    location = projectId.locationId || projectId.location_id;
    asset = projectId.assetId || projectId.asset_id;
  }

  // Handle full resource path passed as projectId
  if (typeof project === 'string' && project.startsWith('projects/')) {
    const parts = project.split('/');
    project = parts[1];
    location = parts[3];
    asset = parts[5];
  }

  // Handle swapped location and project identifiers
  if (
    typeof project === 'string' &&
    /^[a-z]+-[a-z0-9]+$/.test(project) &&
    typeof location === 'string' &&
    !/^[a-z]+-[a-z0-9]+$/.test(location)
  ) {
    const temp = project;
    project = location;
    location = temp;
  }

  const name = client.assetPath
    ? client.assetPath(project, location, asset)
    : `projects/${project}/locations/${location}/assets/${asset}`;

  try {
    const [operation] = await client.deleteAsset({name});
    if (operation && typeof operation.promise === 'function') {
      await operation.promise();
    }

    console.log(`Deleted asset: ${name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Asset not found: ${err.message}`);
    } else if (err.code === status.FAILED_PRECONDITION) {
      console.error(`Precondition failed: ${err.message}`);
    } else {
      console.error(`Failed to delete asset: ${err.message}`);
    }
  }
}

module.exports = {deleteAsset};

What's next

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