Merchant API code sample to delete a notification subscription.
Java
// 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.
package
shopping.merchant.samples.notifications.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.notifications.v1.DeleteNotificationSubscriptionRequest
;
import
com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName
;
import
com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient
;
import
com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/**
* This class demonstrates how to delete a specific Notification Subscription for a given Merchant
* Center account.
*/
public
class
DeleteNotificationSubscriptionSample
{
public
static
void
deleteNotificationSubscription
(
Config
config
)
throws
Exception
{
// Obtains OAuth token based on the user's configuration.
GoogleCredentials
credential
=
new
Authenticator
().
authenticate
();
// Creates service settings using the credentials retrieved above.
NotificationsApiServiceSettings
notificationsApiServiceSettings
=
NotificationsApiServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID. This must match
// the ID used when creating/getting the subscription.
String
notificationSubscriptionId
=
"YOUR_NOTIFICATION_SUBSCRIPTION_ID"
;
// Creates notification subscription name to identify the subscription.
String
name
=
NotificationSubscriptionName
.
newBuilder
()
.
setAccount
(
config
.
getAccountId
().
toString
())
.
setNotificationSubscription
(
notificationSubscriptionId
)
.
build
()
.
toString
();
// Calls the API and catches and prints any network failures/errors.
try
(
NotificationsApiServiceClient
notificationSubscriptionServiceClient
=
NotificationsApiServiceClient
.
create
(
notificationsApiServiceSettings
))
{
// The name has the format:
// accounts/{account}/notificationSubscription/{notification_subscription_id}
DeleteNotificationSubscriptionRequest
request
=
DeleteNotificationSubscriptionRequest
.
newBuilder
().
setName
(
name
).
build
();
System
.
out
.
println
(
"Sending delete Notification Subscription request:"
);
notificationSubscriptionServiceClient
.
deleteNotificationSubscription
(
request
);
System
.
out
.
println
(
"Deleted Notification Subscription: "
+
name
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
deleteNotificationSubscription
(
config
);
}
}
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\Notifications\V1\Client\NotificationsApiServiceClient;
use Google\Shopping\Merchant\Notifications\V1\DeleteNotificationSubscriptionRequest;
/**
* Deletes a Notification Subscription.
*/
class DeleteNotificationSubscription
{
/**
* Deletes a specific Notification Subscription for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @param string $notificationSubscriptionId
* The ID of the notification subscription to delete.
* @throws ApiException
*/
public static function deleteNotificationSubscriptionSample($config, $notificationSubscriptionId): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Set up client options.
$options = ['credentials' => $credentials];
// Create a client instance.
$notificationsApiServiceClient = new NotificationsApiServiceClient($options);
// Construct the resource name.
$name = "accounts/" . $config['accountId'] .
"/notificationsubscriptions/" . $notificationSubscriptionId;
// Create the request object.
$request = new DeleteNotificationSubscriptionRequest(['name' => $name]);
print "Sending delete Notification Subscription request:\n";
// Make the API call.
try {
$notificationsApiServiceClient->deleteNotificationSubscription($request);
print "Deleted Notification Subscription: " . $name . "\n";
} catch (ApiException $e) {
print "Request failed:\n";
print $e->getMessage() . "\n";
}
}
/**
* Execute the sample.
*
* @throws ApiException
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Replace with the actual notification subscription ID.
$notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID";
self::deleteNotificationSubscriptionSample($config, $notificationSubscriptionId);
}
}
// Run the script.
$sample = new DeleteNotificationSubscription();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# 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
#
# 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 NotificationSubscription."""
from
examples.authentication
import
configuration
from
examples.authentication
import
generate_user_credentials
from
google.shopping.merchant_notifications_v1
import
DeleteNotificationSubscriptionRequest
from
google.shopping.merchant_notifications_v1
import
NotificationsApiServiceClient
_ACCOUNT
=
configuration
.
Configuration
()
.
read_merchant_info
()
def
delete_notification_subscription
():
"""Deletes a specific Notification Subscription for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials
=
generate_user_credentials
.
main
()
# Creates a client.
client
=
NotificationsApiServiceClient
(
credentials
=
credentials
)
# Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID.
notification_subscription_id
=
"YOUR_NOTIFICATION_SUBSCRIPTION_ID"
# Creates notification subscription name to identify the subscription.
name
=
f
"accounts/
{
_ACCOUNT
}
/notificationsubscriptions/
{
notification_subscription_id
}
"
# Creates the request.
request
=
DeleteNotificationSubscriptionRequest
(
name
=
name
)
print
(
"Sending delete Notification Subscription request:"
)
# Makes the request and catches and prints any error messages.
try
:
client
.
delete_notification_subscription
(
request
=
request
)
print
(
f
"Deleted Notification Subscription:
{
name
}
"
)
except
RuntimeError
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
delete_notification_subscription
()