# Delete a customer

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

Deletes a specified customer.

This operation schedules the customer resource for deletion, and it is **permanently** deleted after a few minutes.

If you wish to retain the customer data but stop further subscription renewals, consider [canceling](/docs/api/subscriptions/cancel-subscription-for-items) or [pausing](/docs/api/subscriptions/pause-a-subscription) the subscriptions instead.

### Prerequisites & Constraints

-   The customer must not be part of an [account hierarchy](/docs/api/hierarchies) (neither a parent nor a child).
-   The customer record must not have any linked [gift subscriptions](/docs/api/gifts) (neither gifter nor recipient).
-   None of the customer's [invoices](/docs/api/invoices) may be paid by a different customer.
-   None of the customer's [credit notes](/docs/api/credit_notes) may be allocated to an [invoice](/docs/api/invoices) that belongs to a different customer.

### Impacts

**

Subscriptions

**

-   All the subscriptions belonging to the customer are deleted.
-   See [Delete a subscription API](/docs/api/subscriptions/delete-a-subscription) for more details on the impacts of deleting a subscription.

**

Invoices

**

-   All the invoices belonging to the customer are deleted.
-   See [Delete an invoice API](/docs/api/invoices/delete-an-invoice) for more details on the impacts of deleting an invoice.

**

Credit notes

**

-   All the credit notes belonging to the customer are deleted.

**

Payment sources

**

The [payment sources](/docs/api/payment_sources) linked to the customer are deleted and also removed from the [payment gateway](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/gateway_settings). To retain payment gateway records, pass `delete_payment_method` = `false`.

**

Reports

**

-   The numbers in the following [reports](https://www.chargebee.com/docs/billing/2.0/reports-and-analytics/metric_description) are modified when a customer is deleted: Payments, New Revenue, Signups, Activations, Cancellations, and Refunds.

### Implementation Notes

Before deleting a customer, ensure the following:

-   Check and remove account hierarchy linkage:
    1.  Use the [Get hierarchy API](/docs/api/customers/get-hierarchy) to check if the customer is part of an account hierarchy.
    2.  If the customer is part of an account hierarchy, use the [Unlink a customer from its parent account API](/docs/api/customers/delink-a-customer) to remove the customer from the hierarchy.
-   Remove any payments by other customers:
    1.  Use the [List invoices API](/docs/api/invoices/list-invoices) to retrieve all the invoices for this customer.
    2.  For each invoice, look up all the linked payments (`invoice.linked_payments[]`).
    3.  For each linked payment, check if the payer (`transaction.customer_id`) is this customer.
    4.  If not, use the [Remove payment from an invoice API](/docs/api/invoices/remove-payment-from-an-invoice) to remove the payment from the invoice.
-   Remove credit note allocations to other customers:
    1.  Use the [List credit notes API](/docs/api/credit_notes/list-credit-notes) to retrieve all the credit notes for this customer.
    2.  For each credit note, look up all the invoice allocations (`credit_note.allocations.invoice_id`).
    3.  For all such invoices, check if the associated customer (`invoice.customer_id`) is this customer.
    4.  If not, use the [Remove credit note from an invoice API](/docs/api/invoices/remove-credit-note-from-an-invoice) to remove the allocation.
-   Remove credit note allocations from other customers:
    1.  Use the [List invoices API](/docs/api/invoices/list-invoices) to retrieve all the invoices for this customer.
    2.  For each invoice, look up all the applied credit notes (`credit_note.applied_credits.cn_id`).
    3.  For all such credit notes, check if the associated customer (`credit_note.customer_id`) is this customer.
    4.  If not, use the [Remove credit note from an invoice API](/docs/api/invoices/remove-credit-note-from-an-invoice) to remove the credit note allocation.

#### Related APIs

Pause a subscription

Cancel a subscription

Unlink a customer from its parent account

## Sample Request

### Default

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/customers/__test__KyVnHhSBWl9ZA2c8/delete \
     -X POST  \
     -u {site_api_key}:
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Customer.Delete("__test__KyVnHhSBWl9ZA2c8").Request();

Customer customer = result.Customer;
Card card = result.Card;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    customerAction "github.com/chargebee/chargebee-go/v3/actions/customer"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := customerAction.Delete("__test__KyVnHhSBWl9ZA2c8", nil).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.CustomerDeleteRequest{}
  res, err := client.Customer.Delete("__test__KyVnHhSBWl9ZA2c8", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Customer.delete("__test__KyVnHhSBWl9ZA2c8").request();

        Customer customer = result.customer();
        Card card = result.card();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.card.Card;
import com.chargebee.v4.models.customer.Customer;
import com.chargebee.v4.models.customer.params.CustomerDeleteParams;
import com.chargebee.v4.models.customer.responses.CustomerDeleteResponse;

public class CustomerDelete {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        CustomerDeleteResponse response = client.customers().delete("__test__KyVnHhSBWl9ZA2c8");

        Customer customer = response.getCustomer();
        Card card = response.getCard();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.customer.delete("__test__KyVnHhSBWl9ZA2c8");

    console.log(result);
    const customer = result.customer;
    const card = result.card;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->customer()->delete("__test__KyVnHhSBWl9ZA2c8");
$customer = $result->customer;
$card = $result->card;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Customer.delete("__test__KyVnHhSBWl9ZA2c8")
customer = response.customer
card = response.card
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::Customer.delete("__test__KyVnHhSBWl9ZA2c8")

customer = result.customer
card = result.card
```

### Delete a customer without deleting their payment method data from the payment gateway.

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/customers/79460660/delete \
     -u {site_api_key}:\
     -d delete_payment_method="false"
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Customer.Delete("79460660")
		.DeletePaymentMethod(false)
		.Request();

Customer customer = result.Customer;
Card card = result.Card;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    customerAction "github.com/chargebee/chargebee-go/v3/actions/customer"
    "github.com/chargebee/chargebee-go/v3/models/customer"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := customerAction.Delete("79460660", &customer.DeleteRequestParams{
        DeletePaymentMethod : chargebee.Bool(false),
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.CustomerDeleteRequest{
    DeletePaymentMethod : chargebee.Bool(false),
}
  res, err := client.Customer.Delete("79460660", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Customer.delete("79460660")
            .deletePaymentMethod(false)
            .request();

        Customer customer = result.customer();
        Card card = result.card();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.card.Card;
import com.chargebee.v4.models.customer.Customer;
import com.chargebee.v4.models.customer.params.CustomerDeleteParams;
import com.chargebee.v4.models.customer.responses.CustomerDeleteResponse;

public class CustomerDelete {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        CustomerDeleteParams params = CustomerDeleteParams.builder()
            .deletePaymentMethod(false)
            .build();

        CustomerDeleteResponse response = client
            .customers()
            .delete("79460660", params);

        Customer customer = response.getCustomer();
        Card card = response.getCard();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.customer.delete("79460660", {
        delete_payment_method: false
    });

    console.log(result);
    const customer = result.customer;
    const card = result.card;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->customer()->delete("79460660", [
    "delete_payment_method" => false
]);
$customer = $result->customer;
$card = $result->card;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Customer.delete("79460660",
    cb_client.Customer.DeleteParams(
        delete_payment_method=False
    )
)
customer = response.customer
card = response.card
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::Customer.delete("79460660",{
  :delete_payment_method => "false"
})

customer = result.customer
card = result.card
```

## Sample Response

```json
{
  "customer": {
    "allow_direct_debit": false,
    "auto_collection": "on",
    "card_status": "no_card",
    "created_at": 1517505738,
    "deleted": false,
    "excess_payments": 0,
    "first_name": "John",
    "id": "__test__KyVnHhSBWl9ZA2c8",
    "last_name": "Doe",
    "net_term_days": 0,
    "object": "customer",
    "pii_cleared": "active",
    "preferred_currency_code": "USD",
    "promotional_credits": 0,
    "refundable_credits": 0,
    "resource_version": 1517505738000,
    "taxability": "taxable",
    "unbilled_charges": 0,
    "updated_at": 1517505738
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/customers/{customer-id}/delete

## Input Parameters

- `delete_payment_method` (optional, boolean, default=true)
  Deletes the Payment Method from the gateway/vault.

## Returns

- `customer` (Customer object)
  Resource object representing customer

- `card` (Card object)
  Resource object representing card
