skip to Main Content

Wanted to delete all the cards (sources) from the stripe account.
Unable to find any API from documentation to delete the existing cards.

2

Answers


  1. Chosen as BEST ANSWER

    I already got a solution to use the below piece of code to delete all cards from the customer.

    $stripe_secret = env('STRIPE_SECRET');
            $stripe = new StripeStripeClient(
                $stripe_secret
            );
        
            $sources = $stripe->customers->allSources(
                $customerId,
                ['object' => 'card']
            );
            foreach ($sources as $source) {
                $stripe->customers->deleteSource(
                    $customerId,
                    $source->id,
                    []
                );
            }
    

  2. You can use the Stripe API to retrieve a list of all sources for the customer, and then loop through the list to delete each source using its unique identifier.

    use StripeStripe;
    use StripeCustomer;  
    
    Stripe::setApiKey('YOUR_STRIPE_SECRET_KEY');    
    // Retrieve the customer by its ID
    $customer = Customer::retrieve('CUSTOMER_ID');    
    // Retrieve a list of all sources for the customer
    $sources = $customer->sources->all(['object' => 'card']);    
    // Loop through each source and delete it
    foreach($sources->data as $source) {
        $source->delete();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search