Let's Play Bingo in Week 4 of the Ready, Set, Send Challenge!

Deleting Contacts and Bulk Deleting Contacts - PHP API v2

SOLVED
Go to solution
jeffs1089
Campaign Expert

 

Our target group changes daily so maintaining contacts is not necessary.  Right now we clear our lists daily and then send a daily email but there's really no reason for us to have old, un-list-associated contacts sitting around...

 

So - via the site I can delete individual contacts but it looks like via the API I can only change status to 'REMOVED' - can I delete them completely via the API? 

 

Also, is there a way to bulk delete?

 

Thanks!

1 ACCEPTED SOLUTION
jeffs1089
Campaign Expert
0 Votes

 

All makes sense - thanks Dave!

View solution in original post

11 REPLIES 11
DaveBerard
Employee

Contacts are never deleted in our system, including in the UI.  The "Delete" function of the UI simply marks them to not display.  You can still export them and the history of their sends continues to exist.

 

Because the API needs to be able to access these Contacts to update, retrieve information and be associated to reporting information, we provide them all through a status filter of "Removed" as you saw.  There is no way to permanently delete Contacts in the API as there is no way to delete them permanently in Constant Contact.

 

By deleting a list, or using the Bulk Activity to clear a list, you will automatically move your Contacts to a "Removed" state if they are not on any additional lists.  If you are basically just automating clearing a list and adding new contacts to a list, I would highly recommend using our bulk API functions to do this.  It is very simple to do the following pseudo-algorithm:

 

1. Create a Clear Contacts from List Activity: http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-clear-contactlists.htm...

2. Create an Import Contacts Activity with up to 20,000 Contacts or 4MB of data: http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-addcontact-multipart.h...

3. Repeat step 2 if over 20,000k Contacts or 4MB of data until all Contacts are imported http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-addcontact-multipart.h...

 

All bulk activities are processes serially in the order they were created.  So, you can actually simply create all these activities and poll until they all complete to ensure that the process worked correctly.  Hope this helps!

Dave Berard
Senior Product Manager, Constant Contact
jeffs1089
Campaign Expert
0 Votes

 

Thanks for the quick response - just to clarify - the contact total allowed by my account will be assessed against all contacts added and I can never remove or reduce that number?  Or is it assessed by only looking at active contacts?

 

 

 

 

DaveBerard
Employee
0 Votes

Great question and very important one that I should have clarified.  Each bill is determined by the peak number of active contacts in the previous month.  Removed contacts are not used in that calculation with the exception being they were active at some point in time during the previous month. 

Dave Berard
Senior Product Manager, Constant Contact
jeffs1089
Campaign Expert

 

Thanks again - one last question - is there any way to see our monthly peak active contact count?  I can guess it based on our activity, just curious if it's displayed anywhere.

DaveBerard
Employee

That's a great question.  I checked with our billing team to see if that's available in our product and they let me know that it is not available today.  Our typical customer doesn't change very drastically throughout a month or even month to month (usual profile is slow, steady growth over time) and doesn't have generally have a max count that differs from their current Active count, so this is not a feature that our typical customer has requested before.

 

I can see why it would be interesting in your case as you have a highly volatile active list.  We don't currently support sharing that number out but I will bring the feature back to the team and see if this is something we can prioritize in the future.  In full transparency, as this is not something our typical customer runs into it will be a lower priority item unless we start to see higher volume of feedback so I would be remiss if I said it was something we would be getting to soon.

Dave Berard
Senior Product Manager, Constant Contact
jeffs1089
Campaign Expert
0 Votes

 

All makes sense - thanks Dave!

ManiB743
Rookie
0 Votes

Deleting a particular contact from a particular list and that deletion should not affect the other list with the same email. Please provide a example code of it if any?.

David_B.
Employee
0 Votes

Hello,

 

Thank you for reaching out to the API Support team here at Constant Contact.

 

The method for removing an existing contact from a list is to update them, leaving the list you want them removed from off of the list array which would have all other lists they are on. We have an example of a signup form in our PHP SDK which shows the methods for getting a contact's information, as well as submitting an update to the contact. See here for details: https://github.com/constantcontact/php-sdk/blob/master/examples/addOrUpdateContact.php

 

Please let me know if you have any other questions!

 

Sincerely,
David B.
API Support Specialist

ManiB743
Rookie
0 Votes

Please provide a answer for this..

 

Thanks in Advance!!!

ManiB743
Rookie
0 Votes

Deleting a particular contact from a particular list and that deletion should not affect the other list with the same email. Please provide a example code of it if any?

  
 
I need a solution for this..
 
Thanks in advance!!!
 
Jimmy_D
Employee
0 Votes

Hello ManiB743,

 

The sample code can be found by following the link previously shared by David; however I have copy/pasted that code here for easier access. If you plan to use this code as-is you will also want to use the rest of the library as it does come with dependancies. You will want to follow that link and view the readme for full instructions on how to install the library.

 

<!DOCTYPE HTML>
<html>
<head>
    <title>Constant Contact API v2 Add/Update Contact Example</title>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
    <link href="styles.css" rel="stylesheet">
</head>

<!--
README: Add or update contact example
This example flow illustrates how a Constant Contact account owner can add or update a contact in their account. In order for this example to function 
properly, you must have a valid Constant Contact API Key as well as an access token. Both of these can be obtained from 
http://constantcontact.mashery.com.
-->

<?php
// require the autoloaders
require_once '../src/Ctct/autoload.php';
require_once '../vendor/autoload.php';
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Exceptions\CtctException;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "ENTER YOUR API KEY");
define("ACCESS_TOKEN", "ENTER YOUR ACCESS TOKEN");
$cc = new ConstantContact(APIKEY);
// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try {
    $lists = $cc->listService->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
    if (!isset($lists)) {
        $lists = null;
    }
}
// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
    $action = "Getting Contact By Email Address";
    try {
        // check to see if a contact with the email address already exists in the account
        $response = $cc->contactService->getContacts(ACCESS_TOKEN, array("email" => $_POST['email']));
        // create a new contact if one does not exist
        if (empty($response->results)) {
            $action = "Creating Contact";
            $contact = new Contact();
            $contact->addEmail($_POST['email']);
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            /*
             * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant
             * Contact that this action is being performed by the contact themselves, and gives the ability to
             * opt contacts back in and trigger Welcome/Change-of-interest emails.
             *
             * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
             */
            $returnContact = $cc->contactService->addContact(ACCESS_TOKEN, $contact);
            // update the existing contact if address already existed
        } else {
            $action = "Updating Contact";
            $contact = $response->results[0];
            if ($contact instanceof Contact) {
                $contact->addList($_POST['list']);
                $contact->first_name = $_POST['first_name'];
                $contact->last_name = $_POST['last_name'];
                /*
                 * The third parameter of updateContact defaults to false, but if this were set to true it would tell
                 * Constant Contact that this action is being performed by the contact themselves, and gives the ability to
                 * opt contacts back in and trigger Welcome/Change-of-interest emails.
                 *
                 * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
                 */
                $returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
            } else {
                $e = new CtctException();
                $e->setErrors(array("type", "Contact type not returned"));
                throw $e;
            }
        }
        // catch any exceptions thrown during the process and print the errors to screen
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error ' . $action . '</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors());
        echo '</pre></div>';
        die();
    }
}
?>

<body>
<div class="well">
    <h3>Add or Update a Contact</h3>

    <form class="form-horizontal" name="submitContact" id="submitContact" method="POST" action="addOrUpdateContact.php">
        <div class="control-group">
            <label class="control-label" for="email">Email</label>

            <div class="controls">
                <input type="email" id="email" name="email" placeholder="Email Address">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="first_name">First Name</label>

            <div class="controls">
                <input type="text" id="first_name" name="first_name" placeholder="First Name">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="last_name">Last Name</label>

            <div class="controls">
                <input type="text" id="last_name" name="last_name" placeholder="Last Name">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="list">List</label>

            <div class="controls">
                <select name="list">
                    <?php
                    foreach ($lists as $list) {
                        echo '<option value="' . $list->id . '">' . $list->name . '</option>';
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">
                <div class="controls">
                    <input type="submit" value="Submit" class="btn btn-primary"/>
                </div>
        </div>
    </form>
</div>

<!-- Success Message -->
<?php if (isset($returnContact)) {
    echo '<div class="container alert-success"><pre class="success-pre">';
    print_r($returnContact);
    echo '</pre></div>';
} ?>

</body>
</html>

 


Regards,
Jimmy D.
Tier II API Support Engineer
Resources
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up