Deleting subscribers

Deleting a single subscriber

To delete a specific subscriber, you use the NewslettersManager class. The following code deletes a subscriber with the specified IDthrough the Native API.

public void DeleteSubscriber(Guid subscriberId)
{
    NewslettersManager manager = NewslettersManager.GetManager();
 
    if (manager.GetSubscribers().Where(s => s.Id == subscriberId).Count() > 0)
    {
        manager.DeleteSubscriber(subscriberId);
        manager.SaveChanges();
    }
}

First, you initialize the NewslettersManager. Then, you check whether an item with the specified ID exists. Finally, you delete the subscriber by calling DeleteSubscriber, and save the changes.

Deleting all subscribers from a specific list

To delete subscriber forma a specific mailing list, you use the NewslettersManager class. The following code deletes all subscribers from a mailing list through the Native API.

public void DeleteSubscribersFromMailingList(Guid mailingListId)
{
    NewslettersManager manager = NewslettersManager.GetManager();
    MailingList mailingList = manager.GetMailingLists().Where(l => l.Id == mailingListId).SingleOrDefault();
 
    if (mailingList != null)
    {
        List<Subscriber> subscribers = manager.GetSubscribers().Where(s => s.Lists.Contains(mailingList)).ToList();
 
        foreach (var subscriber in subscribers)
        {
            if (subscriber.Lists.Count > 1)
            {
                manager.Unsubscribe(subscriber, mailingListId)
            }
            else
            {
                manager.DeleteSubscriber(subscriber.Id);
            }
        }
 
        manager.SaveChanges();
    }
}

First, you initialize the NewslettersManager. Then, you get the specified mailing list. You get all the subscribers that are enlisted in this mailing list. Then, you iterate the collection and check whether a subscriber is enlisted in other mailing lists. If true, you remove the subscriber from the specified mailing list, otherwise you delete it. Finally, you save the changes.

Deleting all subscribers

To delete all mailing lists, you use the NewslettersManager class. The following code deletes all available mailing lists through the Native API.

public void DeleteSubscribers()
{
    NewslettersManager manager = NewslettersManager.GetManager();
 
    List<Subscriber> subscribers = manager.GetSubscribers().ToList();
 
    foreach (var subscriber in subscribers)
    {
        manager.DeleteSubscriber(subscriber.Id);
    }
 
    manager.SaveChanges();
}

First, you initialize the NewslettersManager. Then, you get all the available subscribers. Then, you iterate the collection and delete the subscribers one by one. Finally, you save the changes.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK