magento2new
magento2new

Reputation: 517

Mailchimp API : Batch Delete subscribers

Is there any reference to the PHP warpper that I can use to perform batch delete subscribers. we have around 100k+ spam subscribers in Mailchimp list that we need to delete using batch delete.

Thanks

Upvotes: 0

Views: 1310

Answers (1)

Joel H.
Joel H.

Reputation: 690

There is no official PHP wrapper for API v3, but you can use third-party wrappers such as this one from DrewM. He has provided good documentation on how to use it.

Here is an example of how you can create a batch operation to delete (not unsubscribe) each spam address in the $spamAddresses array. Of course, you'd have to populate the array first.

<?php
include('mailchimp-api-master/src/MailChimp.php');
include('mailchimp-api-master/src/Batch.php');

use \DrewM\MailChimp\MailChimp;
use \DrewM\MailChimp\Batch;

$apiKey = '********************************';
$listId = '**********';

$spamAddresses = [];

$MailChimp = new MailChimp($apiKey);
$Batch = $MailChimp->new_batch();

//Loop through array of spam addresses.
for($i = 0; $i < sizeof($spamAddresses); $i++){
    $subscriberHash = $MailChimp->subscriberHash($spamAddresses[$i]);
    $Batch->delete("op$i", "lists/$listId/members/$subscriberHash");
}

//Execute batch operation.
$result = $Batch->execute();
echo $result['id'];
?>

Make sure to grab the batch ID that's stored in $result['id'] if you want to check up on the status of the batch operation later, as DrewM's example shows in his documentation:

$MailChimp->new_batch($batch_id);
$result = $Batch->check_status();

Upvotes: 0

Related Questions