Delete a Customer

Learn how to delete a Customer with the commercetools SDKs.

  • After completing this page, you should be able to:

    • Identify the correct process for deleting a Customer with the commercetools SDKs.
  • Our customer John Doe has requested to be removed from our webshop. Let's comply with his wish and delete him. If you have to follow such requests, make sure you read our advice on complying with public regulations such as the European Union's Data Protection Regulation (GDPR).

    To delete a resource in Composable Commerce, you identify the resource with its key or id and then send a DELETE call when using the REST API, together with the correct version. For Customers, see our documentation on deleting Customers.

    An example request would be:

    DELETE https://api.{region}.commercetools.com/{projectKey}/customers/key={key}?version={version}
    Authorization: Bearer my_token

    TypeScript SDK

    Create a file called customerDelete.js and copy the following code:

    import { apiRoot } from '../impl/apiClient.js'; // Update to map to your API root
    const customerKey = 'john-doe-example';
    // Change the version number if necessary
    const customerVersion = 1;
    async function customerDeleteByKey(customerKey, customerVersion) {
    try {
    const response = await apiRoot
    .customers()
    .withKey({ key: customerKey })
    .delete({
    queryArgs: {
    version: customerVersion,
    },
    })
    .execute();
    console.log('Success', JSON.stringify(response.body, null, 2));
    } catch (error) {
    console.log(JSON.stringify(error, null, 2));
    }
    }
    customerDeleteByKey(customerKey, customerVersion);

    Java SDK

    In the following code we have combined the code snippets from the service and exercise class. Make sure you place the code into the correct files and add import statements as needed.

    CustomerService:
    public CompletableFuture<ApiHttpResponse<Customer>> deleteCustomerByKey(String customerKey, long version) {
    return
    apiRoot
    .customers()
    .withKey(customerKey)
    .delete()
    .withVersion(version)
    .execute();
    }
    CustomerExercise:
    logger.info("Customer deleted: " +
    customerService
    .deleteCustomerByKey(
    "john-doe-example", 1L
    )
    .toCompletableFuture().get()
    .getBody().getEmail()
    );

    Don’t forget to verify in the Merchant Center or programmatically that the Customer has indeed been deleted.

    Test your knowledge