Page Summary
-
This webpage provides Java and Python code samples demonstrating how to delete a region from a Merchant Center account using the Merchant API.
-
The Java code sample uses the
RegionsServiceClientto send aDeleteRegionRequestand delete a region specified by its name. -
The Python code sample uses the
RegionsServiceClientand theDeleteRegionRequestto delete a designated region. -
Both code samples require OAuth credentials for authentication and use the Region's name, formatted as
accounts/{account}/region/{regionId}, to identify the region for deletion. -
Successful deletion, shown in both Java and Python examples, does not return a response but prints a success message.
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.
package
shopping.merchant.samples.accounts.regions.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.accounts.v1.DeleteRegionRequest
;
import
com.google.shopping.merchant.accounts.v1.RegionName
;
import
com.google.shopping.merchant.accounts.v1.RegionsServiceClient
;
import
com.google.shopping.merchant.accounts.v1.RegionsServiceSettings
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to delete a given region from a Merchant Center account. */
public
class
DeleteRegionSample
{
public
static
void
deleteRegion
(
Config
config
,
String
region
)
throws
Exception
{
// Obtains OAuth token based on the user's configuration.
GoogleCredentials
credential
=
new
Authenticator
().
authenticate
();
// Creates service settings using the credentials retrieved above.
RegionsServiceSettings
regionsServiceSettings
=
RegionsServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Calls the API and catches and prints any network failures/errors.
try
(
RegionsServiceClient
regionsServiceClient
=
RegionsServiceClient
.
create
(
regionsServiceSettings
))
{
DeleteRegionRequest
request
=
DeleteRegionRequest
.
newBuilder
().
setName
(
region
).
build
();
System
.
out
.
println
(
"Sending Delete Region request"
);
regionsServiceClient
.
deleteRegion
(
request
);
// no response returned on success
System
.
out
.
println
(
"Delete successful."
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
// Creates Region name to identify the Region you want to delete.
// Format: `accounts/{account}/region/{regionId}`
String
regionId
=
"{INSERT_REGION_HERE}"
;
String
region
=
RegionName
.
newBuilder
()
.
setAccount
(
config
.
getAccountId
().
toString
())
.
setRegion
(
regionId
)
.
build
()
.
toString
();
deleteRegion
(
config
,
region
);
}
}
PHP
< ?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();
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 Region."""
from
examples.authentication
import
configuration
from
examples.authentication
import
generate_user_credentials
from
google.shopping.merchant_accounts_v1
import
DeleteRegionRequest
from
google.shopping.merchant_accounts_v1
import
RegionsServiceClient
_ACCOUNT
=
configuration
.
Configuration
()
.
read_merchant_info
()
def
delete_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."
)
except
RuntimeError
as
e
:
print
(
"Delete failed"
)
print
(
e
)
if
__name__
==
"__main__"
:
region_id
=
"123456AB"
delete_region
(
region_id
)

