Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon can I bulk delete subscribers from multiple groups at once using the Mailchimp API
    Mailchimp API bulk delete subscribers remove subscribers Mailchimp PHP example batch operation list management


can I bulk delete subscribers from multiple groups at once using the Mailchimp API


Yes, you can bulk delete subscribers from multiple groups at once using the Mailchimp API. Here are the steps:

1. Identify the Subscribers

1. Use the `lists/{list_id}/members` endpoint to get a list of all subscribers in the list.
2. Filter the list to include only the subscribers you want to delete.

2. Create a Batch Operation

1. Use the `lists/{list_id}/members/batch` endpoint to create a batch operation.
2. Specify the `action` as `delete` and the `ids` as the subscriber IDs you want to delete.

3. Execute the Batch Operation

1. Use the `lists/{list_id}/members/batch/{batch_id}` endpoint to execute the batch operation.
2. The batch operation will delete the specified subscribers from the list.

Example PHP Code

Here's an example of how you can use the Mailchimp API to bulk delete subscribers from multiple groups at once in PHP:

php
<?php
require_once 'path/to/mailchimp-api/vendor/autoload.php';

use \DrewM\MailChimp\MailChimp;

// Initialize the Mailchimp API client
$mailchimp = new MailChimp(MAILCHIMP_API_KEY);
$mailchimp->setConfig(['server' => 'YOUR_SERVER_PREFIX']);

// Get the list ID
$listId = '**********';

// Get the subscriber IDs
$subscriberIds = array_map('md5', array_filter(array_map('strtolower', array_map('trim', array_map('explode', ',', '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy90006 = '&#117;s&#101;r1' + '&#64;';
 addy90006 = addy90006 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy90006 + suffix + '\'' + attribs + '>' );
 document.write( addy90006 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>, 
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy71917 = '&#117;s&#101;r2' + '&#64;';
 addy71917 = addy71917 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy71917 + suffix + '\'' + attribs + '>' );
 document.write( addy71917 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>, 
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy65973 = '&#117;s&#101;r3' + '&#64;';
 addy65973 = addy65973 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy65973 + suffix + '\'' + attribs + '>' );
 document.write( addy65973 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>')))));

// Create a batch operation
$batch = $mailchimp->new_batch();
foreach ($subscriberIds as $subscriberId) {
    $batch->delete("op" . count($batch->operations), "lists/$listId/members/$subscriberId");
}

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

Conclusion

By following these steps and using the Mailchimp API, you can bulk delete subscribers from multiple groups at once. This allows you to automate tasks such as removing spam subscribers from your list.

Citations:
[1] https://stackoverflow.com/questions/46047767/mailchimp-api-batch-delete-subscribers
[2] https://stackoverflow.com/questions/39906432/can-i-use-mailchimps-api-3-0-to-remove-a-large-number-of-subscribers-from-an-in
[3] https://mailchimp.com/help/remove-subscribers-from-groups/
[4] https://mailchimp.com/developer/marketing/api/campaigns/delete-campaign/
[5] https://community.zapier.com/how-do-i-3/how-to-remove-user-from-a-mailchimp-group-but-keep-other-groups-selected-10714