Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to delete a region.
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.packageshopping.merchant.samples.accounts.regions.v1;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.shopping.merchant.accounts.v1.DeleteRegionRequest;importcom.google.shopping.merchant.accounts.v1.RegionName;importcom.google.shopping.merchant.accounts.v1.RegionsServiceClient;importcom.google.shopping.merchant.accounts.v1.RegionsServiceSettings;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** This class demonstrates how to delete a given region from a Merchant Center account. */publicclassDeleteRegionSample{publicstaticvoiddeleteRegion(Configconfig,Stringregion)throwsException{// Obtains OAuth token based on the user's configuration.GoogleCredentialscredential=newAuthenticator().authenticate();// Creates service settings using the credentials retrieved above.RegionsServiceSettingsregionsServiceSettings=RegionsServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Calls the API and catches and prints any network failures/errors.try(RegionsServiceClientregionsServiceClient=RegionsServiceClient.create(regionsServiceSettings)){DeleteRegionRequestrequest=DeleteRegionRequest.newBuilder().setName(region).build();System.out.println("Sending Delete Region request");regionsServiceClient.deleteRegion(request);// no response returned on successSystem.out.println("Delete successful.");}catch(Exceptione){System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=Config.load();// Creates Region name to identify the Region you want to delete.// Format: `accounts/{account}/region/{regionId}`StringregionId="{INSERT_REGION_HERE}";Stringregion=RegionName.newBuilder().setAccount(config.getAccountId().toString()).setRegion(regionId).build().toString();deleteRegion(config,region);}}
<?php/** * Copyright 2025 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\Accounts\V1\Client\RegionsServiceClient;use Google\Shopping\Merchant\Accounts\V1\DeleteRegionRequest;/** * This class demonstrates how to delete a given region from a Merchant Center account. */class DeleteRegionSample{ private static function getParent(string $accountId): string { return sprintf("accounts/%s", $accountId); } public static function deleteRegionSample(array $config, string $regionId): 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]; $parent = self::getParent($config['accountId']); // Creates region name to identify the region. $name = $parent . "/regions/" . $regionId; // Creates a client. $regionsServiceClient = new RegionsServiceClient($options); try { $request = new DeleteRegionRequest([ 'name' => $name ]); print "Sending Delete Region request\n"; $regionsServiceClient->deleteRegion($request); print "Delete successful.\n"; } catch (ApiException $e) { print $e->getMessage(); } } public function callSample(): void { $config = Config::generateConfig(); // Replace this with the ID of the region to be deleted. $regionId = "[INSERT REGION ID HERE]"; self::deleteRegionSample($config, $regionId); }}$sample = new DeleteRegionSample();$sample->callSample();
# -*- 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 Region."""fromexamples.authenticationimportconfigurationfromexamples.authenticationimportgenerate_user_credentialsfromgoogle.shopping.merchant_accounts_v1importDeleteRegionRequestfromgoogle.shopping.merchant_accounts_v1importRegionsServiceClient_ACCOUNT=configuration.Configuration().read_merchant_info()defdelete_region(region_id_to_delete):"""Deletes a given region from a Merchant Center account."""# Gets OAuth Credentials.credentials=generate_user_credentials.main()# Creates a client.client=RegionsServiceClient(credentials=credentials)# Creates Region name to identify the Region you want to delete.region_name="accounts/"+_ACCOUNT+"/regions/"+region_id_to_delete# Creates the request.request=DeleteRegionRequest(name=region_name)# Makes the request and catches and prints any error messages.try:client.delete_region(request=request)print("Delete successful.")exceptRuntimeErrorase:print("Delete failed")print(e)if__name__=="__main__":region_id="123456AB"delete_region(region_id)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis webpage provides Java and Python code samples demonstrating how to delete a region from a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code sample uses the \u003ccode\u003eRegionsServiceClient\u003c/code\u003e to send a \u003ccode\u003eDeleteRegionRequest\u003c/code\u003e and delete a region specified by its name.\u003c/p\u003e\n"],["\u003cp\u003eThe Python code sample uses the \u003ccode\u003eRegionsServiceClient\u003c/code\u003e and the \u003ccode\u003eDeleteRegionRequest\u003c/code\u003e to delete a designated region.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples require OAuth credentials for authentication and use the Region's name, formatted as \u003ccode\u003eaccounts/{account}/region/{regionId}\u003c/code\u003e, to identify the region for deletion.\u003c/p\u003e\n"],["\u003cp\u003eSuccessful deletion, shown in both Java and Python examples, does not return a response but prints a success message.\u003c/p\u003e\n"]]],["The provided code demonstrates how to delete a region from a Merchant Center account using the Merchant API in Java, PHP, and Python. It involves obtaining OAuth credentials, creating a `RegionsServiceClient`, and formulating a `DeleteRegionRequest`. A region name is constructed using the account ID and region ID. The `deleteRegion` method is then invoked with this request, which sends the request to the API, deleting the specified region. Upon success, a confirmation message is printed.\n"],null,["# Delete a region\n\nMerchant API code sample to delete a region. \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.accounts.regions.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.DeleteRegionRequest;\n import com.google.shopping.merchant.accounts.v1.RegionName;\n import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;\n import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to delete a given region from a Merchant Center account. */\n public class DeleteRegionSample {\n\n public static void deleteRegion(Config config, String region) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n RegionsServiceSettings regionsServiceSettings =\n RegionsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Calls the API and catches and prints any network failures/errors.\n try (RegionsServiceClient regionsServiceClient =\n RegionsServiceClient.create(regionsServiceSettings)) {\n DeleteRegionRequest request = DeleteRegionRequest.newBuilder().setName(region).build();\n\n System.out.println(\"Sending Delete Region request\");\n regionsServiceClient.deleteRegion(request); // no response returned on success\n System.out.println(\"Delete successful.\");\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\n // Creates Region name to identify the Region you want to delete.\n // Format: `accounts/{account}/region/{regionId}`\n String regionId = \"{INSERT_REGION_HERE}\";\n\n String region =\n RegionName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setRegion(regionId)\n .build()\n .toString();\n\n deleteRegion(config, region);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/regions/v1/DeleteRegionSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 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 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\\Accounts\\V1\\Client\\RegionsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\DeleteRegionRequest;\n\n /**\n * This class demonstrates how to delete a given region from a Merchant Center account.\n */\n class DeleteRegionSample\n {\n private static function getParent(string $accountId): string\n {\n return sprintf(\"accounts/%s\", $accountId);\n }\n public static function deleteRegionSample(array $config, string $regionId): 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 $parent = self::getParent($config['accountId']);\n\n // Creates region name to identify the region.\n $name = $parent . \"/regions/\" . $regionId;\n\n // Creates a client.\n $regionsServiceClient = new RegionsServiceClient($options);\n\n try {\n $request = new DeleteRegionRequest([\n 'name' =\u003e $name\n ]);\n\n print \"Sending Delete Region request\\n\";\n $regionsServiceClient-\u003edeleteRegion($request);\n print \"Delete successful.\\n\";\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n public function callSample(): void\n {\n $config = Config::generateConfig();\n\n // Replace this with the ID of the region to be deleted.\n $regionId = \"[INSERT REGION ID HERE]\";\n self::deleteRegionSample($config, $regionId);\n }\n }\n\n\n $sample = new DeleteRegionSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/regions/v1/DeleteRegionSample.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 Region.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import DeleteRegionRequest\n from google.shopping.merchant_accounts_v1 import RegionsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def delete_region(region_id_to_delete):\n \"\"\"Deletes a given region from a Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = RegionsServiceClient(credentials=credentials)\n\n # Creates Region name to identify the Region you want to delete.\n region_name = \"accounts/\" + _ACCOUNT + \"/regions/\" + region_id_to_delete\n\n # Creates the request.\n request = DeleteRegionRequest(name=region_name)\n\n # Makes the request and catches and prints any error messages.\n try:\n client.delete_region(request=request)\n print(\"Delete successful.\")\n except RuntimeError as e:\n print(\"Delete failed\")\n print(e)\n\n\n if __name__ == \"__main__\":\n region_id = \"123456AB\"\n delete_region(region_id)\n\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/regions/v1/delete_region_sample.py"]]