Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon can I use the Mailchimp API to delete subscribers from groups without unsubscribing them first
    Mailchimp API delete subscribers Mailchimp groups unsubscribe PHP code batch operation


can I use the Mailchimp API to delete subscribers from groups without unsubscribing them first


Yes, you can use the Mailchimp API to delete subscribers from groups without unsubscribing them first. 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 from the group.

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 from the group.

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 group without unsubscribing them.

Example PHP Code

Here's an example of how you can use the Mailchimp API to delete subscribers from groups without unsubscribing them 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 addy61064 = '&#117;s&#101;r1' + '&#64;';
 addy61064 = addy61064 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy61064 + suffix + '\'' + attribs + '>' );
 document.write( addy61064 );
 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 addy12983 = '&#117;s&#101;r2' + '&#64;';
 addy12983 = addy12983 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy12983 + suffix + '\'' + attribs + '>' );
 document.write( addy12983 );
 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 addy32922 = '&#117;s&#101;r3' + '&#64;';
 addy32922 = addy32922 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy32922 + suffix + '\'' + attribs + '>' );
 document.write( addy32922 );
 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 delete subscribers from groups without unsubscribing them. This allows you to automate tasks such as removing spam subscribers from specific groups without affecting their subscription status.

Citations:
[1] https://mailchimp.com/developer/marketing/api/lists/batch-subscribe-or-unsubscribe/
[2] https://stackoverflow.com/questions/46047767/mailchimp-api-batch-delete-subscribers
[3] https://mailchimp.com/developer/marketing/api/list-members/delete-list-member/
[4] https://stackoverflow.com/questions/39906432/can-i-use-mailchimps-api-3-0-to-remove-a-large-number-of-subscribers-from-an-in
[5] https://mailchimp.com/help/remove-subscribers-from-groups/