Removing the Newsletter Queue in Magento

Occasionally you may wish to remove newsletter posts that have been queued in Magento, either before or whilst the queue is being processed. Removing the newsletter queue in Magento is no easy task, as there isn’t an action to do so via the admin interface in Magento.

Removing the Newsletter Queue in Magento

There may be several reasons why you would want to remove the queued newsletters. An example might be if you’ve configured a newsletter to be sent for an occasion, such as Christmas. You then find out that for some reason, the Magento cron wasn’t working or wasn’t configured correctly on your server. Christmas passes, and somewhen after, the cron configuration is corrected. Your newsletter remains in the queue, and newsletter emails start to send out to subscribers.

Fortunately, there is a simple script you can use to remove entries.

For simplicity, an external file will be used to bootstrap Magento. Start by placing the file within the Magento root directory, and add the following lines.

require_once __DIR__ . '/app/Mage.php';
Mage::app();

Next, retrieve the newsletter queue collection by adding the following line of code.

$collection = Mage::getResourceModel('newsletter/queue_collection')
    ->addSubscribersInfo();

You can then iterate over the collection, and decide which queue entities to delete based on the queue status.

foreach ($collection->getItems() as $item) {
    if ($item->getStatus() === Mage_Newsletter_Model_Queue::STATUS_NEVER ||
        $item->getStatus() == Mage_Newsletter_Model_Queue::STATUS_CANCEL ||
        $item->getStatus() == Mage_Newsletter_Model_Queue::STATUS_PAUSE) {
        $item->delete();
    }
}

The list of queue statuses can be found within the Mage_Newsletter_Model_Queue class.

const STATUS_NEVER = 0;
const STATUS_SENDING = 1;
const STATUS_CANCEL = 2;
const STATUS_SENT = 3;
const STATUS_PAUSE = 4;

Altogether, the file should look similar to the below.

<?php
require_once __DIR__ . '/app/Mage.php';
Mage::app();

$collection = Mage::getResourceModel('newsletter/queue_collection')
    ->addSubscribersInfo();

foreach ($collection->getItems() as $item) {
    if ($item->getStatus() === Mage_Newsletter_Model_Queue::STATUS_NEVER ||
        $item->getStatus() == Mage_Newsletter_Model_Queue::STATUS_CANCEL ||
        $item->getStatus() == Mage_Newsletter_Model_Queue::STATUS_PAUSE) {
        $item->delete();
    }
}

After running the external PHP file, you should notice that the queue items have been deleted. There isn’t anything that can be done to newsletters that have already been sent out to customers, but using the file you should be able to prevent subscribers from receiving unwanted newsletters.

Note: This article is based on Magento Community/Open Source version 1.9.