# Delete a payment source

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


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

Deletes a payment source. Once the payment source is deleted, if

-   **Deleted payment source is Primary, and Backup is available**
    -   The Backup payment source will become the Primary payment source.
-   **Deleted payment source is Primary, and no Backup is available**
    -   The other payment source available, but not assigned to any subscription, will become the Primary payment source.
        
        **Note**: _When multiple payment sources exist, the payment method added most recently will be considered_.
        
    -   If other payment sources available are assigned to subscriptions, the auto collection attribute for the customer will be set to Off, and the events _card\_deleted_ and _payment\_source\_deleted_ will be triggered.
        
-   **Deleted payment source is attached to subscriptions**
    -   Dunning will be initiated for subscriptions attached to this payment source if auto collection is set to On, and when no customer default is present.

If there is no such payment source present in the gateway for the customer, this API will return successfully without throwing any error.

**Note** :

If you delete the only available payment method of a customer in Chargebee, it also deletes the customer's record at the gateway. To delete the payment method locally(delete only in Chargebee), use [Local Delete a Payment Source API](/docs/api/payment_sources/local-delete-a-payment-source) .

## Sample Request

#### cURL

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

#### .NET

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

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

Customer customer = result.Customer;
PaymentSource paymentSource = result.PaymentSource;
```

#### Go

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

#### 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)
  
  res, err := client.PaymentSource.Delete("pm___test__XpbTXGTSRp4SEvEe")
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        PaymentSource := res.PaymentSource
    }
}
```

#### 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 = PaymentSource.delete("pm___test__XpbTXGTSRp4SEvEe").request();

        Customer customer = result.customer();
        PaymentSource paymentSource = result.paymentSource();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.customer.Customer;
import com.chargebee.v4.models.paymentSource.PaymentSource;
import com.chargebee.v4.models.paymentSource.params.PaymentSourceDeleteParams;
import com.chargebee.v4.models.paymentSource.responses.PaymentSourceDeleteResponse;

public class PaymentSourceDelete {

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

        PaymentSourceDeleteResponse response = client.paymentSources().delete("pm___test__XpbTXGTSRp4SEvEe");

        Customer customer = response.getCustomer();
        PaymentSource paymentSource = response.getPaymentSource();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.paymentSource.delete("pm___test__XpbTXGTSRp4SEvEe");

    console.log(result);
    const customer = result.customer;
    const paymentSource = result.payment_source;
} 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->paymentSource()->delete("pm___test__XpbTXGTSRp4SEvEe");
$customer = $result->customer;
$paymentSource = $result->payment_source;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.PaymentSource.delete("pm___test__XpbTXGTSRp4SEvEe")
customer = response.customer
payment_source = response.payment_source
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::PaymentSource.delete("pm___test__XpbTXGTSRp4SEvEe")

customer = result.customer
payment_source = result.payment_source
```

## Sample Response

```json
{
  "customer": {
    "allow_direct_debit": false,
    "auto_collection": "off",
    "card_status": "no_card",
    "created_at": 1517487236,
    "deleted": false,
    "excess_payments": 0,
    "first_name": "Mark",
    "id": "__test__XpbTXGTSRp4Rx1Ed",
    "last_name": "Henry",
    "net_term_days": 0,
    "object": "customer",
    "pii_cleared": "active",
    "preferred_currency_code": "USD",
    "promotional_credits": 0,
    "refundable_credits": 0,
    "resource_version": 1517487238746,
    "taxability": "taxable",
    "unbilled_charges": 0,
    "updated_at": 1517487238
  },
  "payment_source": {
    "card": {
      "brand": "visa",
      "expiry_month": 12,
      "expiry_year": 2022,
      "funding_type": "credit",
      "iin": "411111",
      "last4": "1111",
      "masked_number": "************1111",
      "object": "card"
    },
    "created_at": 1517487237,
    "customer_id": "__test__XpbTXGTSRp4Rx1Ed",
    "deleted": true,
    "gateway": "stripe",
    "gateway_account_id": "gw___test__5SK2lMpwSRp4Mx02v",
    "id": "pm___test__XpbTXGTSRp4SEvEe",
    "issuing_country": "US",
    "object": "payment_source",
    "reference_id": "cus_J7rVgQok1LkbJ9/card_1IVbmbJv9j0DyntJhbTqsfn1",
    "resource_version": 1517487237828,
    "status": "valid",
    "type": "card",
    "updated_at": 1517487237
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/payment_sources/{cust-payment-source-id}/delete

## Returns

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

- `payment_source` (Payment source object)
  Resource object representing payment\_source
