Delete an existing data exchange in Analytics Hub.

## Code sample

### Go


Before trying this sample, follow the Go setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Go API
reference documentation](https://godoc.org/cloud.google.com/go/bigquery).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```go

import (
	"context"
	"fmt"
	"io"

	analyticshub "cloud.google.com/go/bigquery/analyticshub/apiv1"
	analyticshubpb "cloud.google.com/go/bigquery/analyticshub/apiv1/analyticshubpb"
)

// deleteDataExchange deletes an existing data exchange in Analytics Hub.
func deleteDataExchange(ctx context.Context, w io.Writer, projectID string, locationID string, dataExchangeID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/dataExchanges/%s", projectID, locationID, dataExchangeID)

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

	req := &analyticshubpb.DeleteDataExchangeRequest{
		Name: name,
	}

	if err := client.DeleteDataExchange(ctx, req); err != nil {
		return fmt.Errorf("DeleteDataExchange: %w", err)
	}

	fmt.Fprintf(w, "Deleted data exchange: %s\n", name)
	return nil
}
```

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```java
import com.google.cloud.bigquery.analyticshub.v1.AnalyticsHubServiceClient;
import com.google.cloud.bigquery.analyticshub.v1.DataExchangeName;
import java.io.IOException;

public class DeleteDataExchange {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    String dataExchangeId = "example-data-exchange-id";
    deleteDataExchange(projectId, locationId, dataExchangeId);
  }

  public static void deleteDataExchange(String projectId, String locationId, String dataExchangeId)
      throws IOException {
    // Initialize client that sends requests. Create once and reuse for multiple requests.
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String name = DataExchangeName.of(projectId, locationId, dataExchangeId).toString();

      client.deleteDataExchange(name);
      System.out.println("Data exchange deleted successfully: " + name);
    }
  }
}
```

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```javascript
'use strict';

const {AnalyticsHubServiceClient} = require('@google-cloud/bigquery-analyticshub');
const {status} = require('@grpc/grpc-js');

const client = new AnalyticsHubServiceClient();

/**
 * Deletes an existing data exchange.
 *
 * @param {string} projectId - The Google Cloud project ID containing the data exchange.
 * @param {string} locationId - The Google Cloud region or location where the data exchange resides (for example, 'us' or 'us-central1').
 * @param {string} dataExchangeId - The ID of the data exchange to delete.
 */
async function deleteDataExchange(projectId, locationId, dataExchangeId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/dataExchanges/${dataExchangeId}`;

    const request = {
      name,
    };

    await client.deleteDataExchange(request);
    console.log(`Deleted data exchange: ${name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Data exchange not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else {
      console.error(`Failed to delete data exchange: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { deleteDataExchange };
```

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```python
import google.api_core.exceptions
from google.cloud import bigquery_analyticshub_v1


def delete_data_exchange(
    project_id: str, location_id: str, data_exchange_id: str
) -> None:
    """Deletes a data exchange in Analytics Hub.

    Args:
        project_id: The Google Cloud project ID hosting the data exchange.
        location_id: The Google Cloud region or location of the data exchange
            (for example, 'us' or 'us-central1').
        data_exchange_id: The ID of the data exchange to delete.
    """
    client = bigquery_analyticshub_v1.AnalyticsHubServiceClient()

    name = (
        f"projects/{project_id}/locations/{location_id}/"
        f"dataExchanges/{data_exchange_id}"
    )

    request = bigquery_analyticshub_v1.DeleteDataExchangeRequest(name=name)

    try:
        client.delete_data_exchange(request=request)
        print(f"Deleted data exchange: {name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to delete data exchange: {error}")
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=bigquery).