Delete a user

  • This webpage provides code samples in Java, PHP, and Python demonstrating how to delete a user from a Merchant Center account using the Merchant API.

  • Each code sample uses OAuth credentials to authenticate and interact with the UserServiceClient.

  • The code samples create a DeleteUserRequest object containing the user's name (constructed from account ID and email) to specify which user to delete.

  • The API call deleteUser is then executed, and a successful operation does not return any response, only confirming the action, while any errors are caught and handled.

  • The variable used to identify the user being deleted can either be an email address or "me" to delete the user making the request, and in each code language, it is set to be "testUser@gmail.com", or "USER_MAIL_ACCOUNT" in the case of Python.

Merchant API code sample to delete a user.

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.users.v1 
 ; 
 import 
  
 com.google.api.gax.core.FixedCredentialsProvider 
 ; 
 import 
  
 com.google.auth.oauth2.GoogleCredentials 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.DeleteUserRequest 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.UserName 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.UserServiceClient 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.UserServiceSettings 
 ; 
 import 
  
 shopping.merchant.samples.utils.Authenticator 
 ; 
 import 
  
 shopping.merchant.samples.utils.Config 
 ; 
 /** This class demonstrates how to delete a user for a given Merchant Center account. */ 
 public 
  
 class 
 DeleteUserSample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 deleteUser 
 ( 
 Config 
  
 config 
 , 
  
 String 
  
 email 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // Obtains OAuth token based on the user's configuration. 
  
 GoogleCredentials 
  
 credential 
  
 = 
  
 new 
  
 Authenticator 
 (). 
 authenticate 
 (); 
  
 // Creates service settings using the credentials retrieved above. 
  
 UserServiceSettings 
  
 userServiceSettings 
  
 = 
  
 UserServiceSettings 
 . 
 newBuilder 
 () 
  
 . 
 setCredentialsProvider 
 ( 
 FixedCredentialsProvider 
 . 
 create 
 ( 
 credential 
 )) 
  
 . 
 build 
 (); 
  
 // Creates user name to identify the user. 
  
 String 
  
 name 
  
 = 
  
 UserName 
 . 
 newBuilder 
 () 
  
 . 
 setAccount 
 ( 
 config 
 . 
 getAccountId 
 (). 
 toString 
 ()) 
  
 . 
 setEmail 
 ( 
 email 
 ) 
  
 . 
 build 
 () 
  
 . 
 toString 
 (); 
  
 // Calls the API and catches and prints any network failures/errors. 
  
 try 
  
 ( 
 UserServiceClient 
  
 userServiceClient 
  
 = 
  
 UserServiceClient 
 . 
 create 
 ( 
 userServiceSettings 
 )) 
  
 { 
  
 DeleteUserRequest 
  
 request 
  
 = 
  
 DeleteUserRequest 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending Delete User request" 
 ); 
  
 userServiceClient 
 . 
 deleteUser 
 ( 
 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 
 (); 
  
 // The email address of this user. If you want to delete the user information 
  
 // Of the user making the Content API request, you can also use "me" instead 
  
 // Of an email address. 
  
 String 
  
 email 
  
 = 
  
 "testUser@gmail.com" 
 ; 
  
 deleteUser 
 ( 
 config 
 , 
  
 email 
 ); 
  
 } 
 } 
  
 

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. 
 */ 
 /** 
 * Demonstrates how to delete a user for a given Merchant Center account. 
 */ 
 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\DeleteUserRequest; 
 use Google\Shopping\Merchant\Accounts\V1\Client\UserServiceClient; 
 /** 
 * Deletes a user. 
 * 
 * @param array $config The configuration data. 
 * @param string $email The email address of the user. 
 * @return void 
 */ 
 function deleteUser($config, $email): 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. 
 $userServiceClient = new UserServiceClient($options); 
 // Creates user name to identify the user. 
 $name = 'accounts/' . $config['accountId'] . "/users/" . $email; 
 // Calls the API and catches and prints any network failures/errors. 
 try { 
 $request = new DeleteUserRequest(['name' => $name]); 
 print "Sending Delete User request\n"; 
 $userServiceClient->deleteUser($request); 
 print "Delete successful.\n"; 
 } catch (ApiException $e) { 
 print $e->getMessage(); 
 } 
 } 
 $config = Config::generateConfig(); 
 $email = "testUser@gmail.com"; 
 deleteUser($config, $email);  
 
 

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 user.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.shopping.merchant_accounts_v1 
  
 import 
 DeleteUserRequest 
 from 
  
 google.shopping.merchant_accounts_v1 
  
 import 
 UserServiceClient 
 _ACCOUNT 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 def 
  
 delete_user 
 ( 
 user_email 
 ): 
  
 """Deletes a user for a given Merchant Center account.""" 
 # Get OAuth credentials 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Create a UserServiceClient 
 client 
 = 
 UserServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Create user name string 
 name 
 = 
 "accounts/" 
 + 
 _ACCOUNT 
 + 
 "/users/" 
 + 
 user_email 
 # Create the request 
 request 
 = 
 DeleteUserRequest 
 ( 
 name 
 = 
 name 
 ) 
 try 
 : 
 print 
 ( 
 "Sending Delete User request" 
 ) 
 client 
 . 
 delete_user 
 ( 
 request 
 = 
 request 
 ) 
 print 
 ( 
 "Delete successful." 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 # Modify this email to delete the right user 
 email 
 = 
 "USER_MAIL_ACCOUNT" 
 delete_user 
 ( 
 email 
 ) 
  
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: