Menghapus sumber data
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Contoh kode Merchant API untuk menghapus sumber data.
Java
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.merchant.samples.datasources.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1.DataSourceName;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1.DeleteDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a datasource. */
public class DeleteDataSourceSample {
public static void deleteDataSource(Config config, String dataSourceId) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name =
DataSourceName.newBuilder()
.setAccount(config.getAccountId().toString())
.setDatasource(dataSourceId)
.build()
.toString();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
DeleteDataSourceRequest request = DeleteDataSourceRequest.newBuilder().setName(name).build();
System.out.println("Sending deleteDataSource request");
// Delete works for any datasource type.
// If Type "Supplemental", delete will only work if it's not linked to any primary feed.
// If a link exists and the Type is "Supplemental", you will need to remove the supplemental
// feed from the default and/or custom rule(s) of any primary feed(s) that references it. Then
// retry the delete.
dataSourcesServiceClient.deleteDataSource(request); // No response returned on success.
System.out.println(
"Delete successful, note that it may take a few minutes for the delete to update in"
+ " the system.");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID automatically assigned to the datasource after creation by Google.
String dataSourceId = "1111111111"; // Replace with your datasource ID.
deleteDataSource(config, dataSourceId);
}
}
PHP
<?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/../../Authentication/Authentication.php';
require_once __DIR__ . '/../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\DataSources\V1\Client\DataSourcesServiceClient;
use Google\Shopping\Merchant\DataSources\V1\DeleteDataSourceRequest;
/**
* Class to demonstrate deleting a datasource.
*/
class DeleteDataSourceSample
{
// ENSURE you fill in the datasource ID for the sample to work.
private const DATASOURCE_ID = 'INSERT_DATASOURCE_ID';
/**
* Deletes a DataSource.
*
* @param int $merchantId The Merchant Center Account ID.
* @param string $dataSourceId The data source ID.
*/
public function deleteDataSource(int $merchantId, string $dataSourceId): void
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$dataSourcesServiceClient = new DataSourcesServiceClient($options);
// Creates the data source name.
$name = sprintf('accounts/%s/dataSources/%s', $merchantId, $dataSourceId);
// Creates the request.
$request = (new DeleteDataSourceRequest())
->setName($name);
print('Sending deleteDataSource request' . PHP_EOL);
// Calls the API and catches and prints any network failures/errors.
try {
$dataSourcesServiceClient->deleteDataSource($request);
print('Delete successful, note that it may take a few minutes for the delete to update in the system.' . PHP_EOL);
} catch (ApiException $ex) {
print('Call failed with message: ' . $ex->getMessage() . PHP_EOL);
}
}
// Helper to execute the sample.
public function callSample(): void
{
$config = Config::generateConfig();
// The Merchant Center Account ID.
$merchantId = $config['accountId'];
self::deleteDataSource($merchantId, self::DATASOURCE_ID);
}
}
$sample = new DeleteDataSourceSample();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to delete a DataSource."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping import merchant_datasources_v1
# ENSURE you fill in the datasource ID for the sample to
# work.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# An ID automatically assigned to the datasource after creation by Google.
_DATASOURCE = "[INSERT_DATASOURCE_HERE]"
_NAME = f"accounts/{_ACCOUNT}/dataSources/{_DATASOURCE}"
def delete_data_source():
"""Deletes the specified `DataSource` resource.
Delete works for any datasource type.
If Type "Supplemental", delete will only work if it's not linked to any
primary feed. If a link exists and the Type is "Supplemental", you will need
to remove the supplemental feed from the default and/or custom rule(s) of any
primary feed(s) that references it. Then retry the delete.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = merchant_datasources_v1.DataSourcesServiceClient(
credentials=credentials
)
# Creates the request.
request = merchant_datasources_v1.DeleteDataSourceRequest(name=_NAME)
# Makes the request and catches and prints any error messages.
try:
# No response is returned on request.
client.delete_data_source(request=request)
print("Deletion successful")
except RuntimeError as e:
print("Deletion failed")
print(e)
if __name__ == "__main__":
delete_data_source()
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-21 UTC.
[null,null,["Terakhir diperbarui pada 2025-08-21 UTC."],[[["\u003cp\u003eThis webpage provides code samples in Java, PHP, and Python demonstrating how to delete a data source using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eDeleting a data source requires the data source ID and proper authentication credentials for the Merchant Center account.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples show how to create and send a \u003ccode\u003eDeleteDataSourceRequest\u003c/code\u003e to the Merchant API's \u003ccode\u003eDataSourcesServiceClient\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDeletion is successful when there is no response provided, but may take a few minutes for changes to update in the system.\u003c/p\u003e\n"],["\u003cp\u003eFor supplemental data sources, deletion will only be possible if it's not linked to any primary feed, otherwise the link will need to be removed first.\u003c/p\u003e\n"]]],[],null,["# Delete a data source\n\nMerchant API code sample to delete a data source. \n\n### Java\n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.datasources.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.datasources.v1.DataSourceName;\n import com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient;\n import com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings;\n import com.google.shopping.merchant.datasources.v1.DeleteDataSourceRequest;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to delete a datasource. */\n public class DeleteDataSourceSample {\n\n public static void deleteDataSource(Config config, String dataSourceId) throws Exception {\n GoogleCredentials credential = new Authenticator().authenticate();\n\n DataSourcesServiceSettings dataSourcesServiceSettings =\n DataSourcesServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n String name =\n DataSourceName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setDatasource(dataSourceId)\n .build()\n .toString();\n\n try (DataSourcesServiceClient dataSourcesServiceClient =\n DataSourcesServiceClient.create(dataSourcesServiceSettings)) {\n DeleteDataSourceRequest request = DeleteDataSourceRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending deleteDataSource request\");\n // Delete works for any datasource type.\n // If Type \"Supplemental\", delete will only work if it's not linked to any primary feed.\n // If a link exists and the Type is \"Supplemental\", you will need to remove the supplemental\n // feed from the default and/or custom rule(s) of any primary feed(s) that references it. Then\n // retry the delete.\n\n dataSourcesServiceClient.deleteDataSource(request); // No response returned on success.\n System.out.println(\n \"Delete successful, note that it may take a few minutes for the delete to update in\"\n + \" the system.\");\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n // An ID automatically assigned to the datasource after creation by Google.\n String dataSourceId = \"1111111111\"; // Replace with your datasource ID.\n\n deleteDataSource(config, dataSourceId);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/datasources/v1/DeleteDataSourceSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n require_once __DIR__ . '/../../../vendor/autoload.php';\n require_once __DIR__ . '/../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\DataSources\\V1\\Client\\DataSourcesServiceClient;\n use Google\\Shopping\\Merchant\\DataSources\\V1\\DeleteDataSourceRequest;\n\n /**\n * Class to demonstrate deleting a datasource.\n */\n class DeleteDataSourceSample\n {\n // ENSURE you fill in the datasource ID for the sample to work.\n private const DATASOURCE_ID = 'INSERT_DATASOURCE_ID';\n\n /**\n * Deletes a DataSource.\n *\n * @param int $merchantId The Merchant Center Account ID.\n * @param string $dataSourceId The data source ID.\n */\n public function deleteDataSource(int $merchantId, string $dataSourceId): void\n {\n // Gets the OAuth credentials to make the request.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Creates options config containing credentials for the client to use.\n $options = ['credentials' =\u003e $credentials];\n\n // Creates a client.\n $dataSourcesServiceClient = new DataSourcesServiceClient($options);\n\n // Creates the data source name.\n $name = sprintf('accounts/%s/dataSources/%s', $merchantId, $dataSourceId);\n\n // Creates the request.\n $request = (new DeleteDataSourceRequest())\n -\u003esetName($name);\n\n print('Sending deleteDataSource request' . PHP_EOL);\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n $dataSourcesServiceClient-\u003edeleteDataSource($request);\n print('Delete successful, note that it may take a few minutes for the delete to update in the system.' . PHP_EOL);\n } catch (ApiException $ex) {\n print('Call failed with message: ' . $ex-\u003egetMessage() . PHP_EOL);\n }\n }\n\n // Helper to execute the sample.\n public function callSample(): void\n {\n $config = Config::generateConfig();\n // The Merchant Center Account ID.\n $merchantId = $config['accountId'];\n\n self::deleteDataSource($merchantId, self::DATASOURCE_ID);\n }\n }\n\n $sample = new DeleteDataSourceSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/datasources/v1/DeleteDataSourceSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"A module to delete a DataSource.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping import merchant_datasources_v1\n\n # ENSURE you fill in the datasource ID for the sample to\n # work.\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n # An ID automatically assigned to the datasource after creation by Google.\n _DATASOURCE = \"[INSERT_DATASOURCE_HERE]\"\n _NAME = f\"accounts/{_ACCOUNT}/dataSources/{_DATASOURCE}\"\n\n\n def delete_data_source():\n \"\"\"Deletes the specified `DataSource` resource.\n\n Delete works for any datasource type.\n If Type \"Supplemental\", delete will only work if it's not linked to any\n primary feed. If a link exists and the Type is \"Supplemental\", you will need\n to remove the supplemental feed from the default and/or custom rule(s) of any\n primary feed(s) that references it. Then retry the delete.\n \"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = merchant_datasources_v1.DataSourcesServiceClient(\n credentials=credentials\n )\n\n # Creates the request.\n request = merchant_datasources_v1.DeleteDataSourceRequest(name=_NAME)\n\n # Makes the request and catches and prints any error messages.\n try:\n # No response is returned on request.\n client.delete_data_source(request=request)\n print(\"Deletion successful\")\n except RuntimeError as e:\n print(\"Deletion failed\")\n print(e)\n\n\n if __name__ == \"__main__\":\n delete_data_source()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/datasources/v1/delete_data_source_sample.py"]]