LogoLogo
API DocsTemplate GalleryProduct UpdatesContact Us
  • 📬Print & Mail
    • Ready to get started?
      • Pricing details
      • Fast Track Guide
    • Integrations
      • API integrations
        • Action IQ
        • Adobe Marketo Engage
        • Blueshift
        • Braze
        • Customer.io
        • Hubspot
        • Iterable
        • Jotform
        • Klayvio
        • Listrak
        • Make
        • Optimove
        • Salesforce
        • Salesforce Marketing Cloud
        • Segment
        • Simon Data
        • Zapier
      • No-code integrations
        • Agile CRM
        • Freshsales Suite
        • HubSpot
        • Microsoft Dynamics 365
        • Pipedrive
        • Salesforce
        • Salesforce Marketing Cloud
        • Zoho
      • Creative conversion tools
        • Figma plugin
    • Reaching your audience
      • Lob Audience
        • Target Audiences
        • Lookalike Audiences
        • Purchasing Audiences
      • All about addresses
      • Campaign audience guide
      • Additional Lob NCOA functionality
    • Designing mail creatives
      • Artboard layout
      • Creative formatting
        • PDF preflight checklist
        • Exporting PDFs from InDesign
        • Rendering errors
      • Mail piece design specs
        • Postcards
        • Self-Mailers
        • Letters
        • Letter envelopes
        • Letter add-ons
        • Checks
        • Snap packs
        • Booklets
        • Custom mail
      • Maximizing engagement
        • Dynamic personalization
          • Advanced templating (Handlebars)
            • Dynamic table tutorial
        • Adding QR codes
        • Short URLs
        • Informed Delivery
    • Building a mail strategy
      • One-time campaigns or triggered sends?
      • Choosing a delivery strategy
      • Managing mail settings
        • Using metadata
        • Declaring mail use type
      • USPS Promotions Through Lob
        • Tactile, Sensory & Interactive Promotion
        • Integrated Technology Promotion
        • First Class Mail Advertising Promotion
        • Add-Ons
      • Mailing class and postage
        • Certified Mail or Registered Mail
      • International mail
    • Send mail!
      • Launch your first campaign
      • Send mail via Print & Mail API
      • Send campaigns via the Campaigns API
      • USPS Secure Destruction
    • Getting data & results
      • Tracking your mail
      • Mail analytics
      • Measuring attribution
      • Using webhooks
      • Exporting mail data
  • 🏠Address Verification
    • Ready to start AV?
      • US AV product suite
      • International AV suite
      • AV pricing
    • AV best practices
    • AV integrations & libraries
      • AV Elements
      • Shopify App: Address Cleanser
    • AV FAQs
  • 💻Developer Docs
    • API quickstart guide
    • SDKs & libraries
    • Postman & similar tools
    • Error reference
    • Upgrading API version
    • Technical use case guides
      • Mass deletion setup
      • NCOA responses
      • Override cancellation window
      • Visibility of address changes
      • Ingesting tracking events with webhooks
  • 🔑Account Management
    • Signing into Lob
    • API keys
    • Account settings
      • Account-level data logs
    • User settings
    • Billing
      • Lob Credits
      • Lob Payment Methods
      • Sales Tax FAQ
        • Applicable sales tax by state
          • ​Subscriptions and Services
          • Lob Audience
          • Delivery Location for Operational Mail
          • Customer Billing Address for Operational Mail
          • Delivery Location for Marketing Mail
          • Postage Exemption
          • Professional Services
        • Tax exemption guide
  • 📞Resources
    • Getting support
    • Security & privacy
    • Data retention
    • Sustainability
    • Private labeling Lob
    • Direct mail glossary
Powered by GitBook
On this page
  • Overview
  • Prerequisites
  • Mass deletion using Lob metadata
  • Mass deletion utilizing your own infrastructure

Was this helpful?

Export as PDF
  1. Developer Docs
  2. Technical use case guides

Mass deletion setup

PreviousTechnical use case guidesNextNCOA responses

Last updated 8 months ago

Was this helpful?

Please contact if you need help with live mail pieces already created. Note:

  • If the has already elapsed, canceling is not possible.

  • If not elapsed, you will need to provide a list of the resource IDs you would like to have canceled.

  • Submitting deletion requests via Support is not a suitable replacement for making deletion requests on your own.

Overview

Having a mass delete system in place is essential to avoid unnecessary costs, save time, and protect customer relationships when a campaign is mistakenly triggered. This feature allows you to cancel large amounts of mail associated with a specific campaign, identified by a batch ID, without disrupting other outgoing mail.

In this tutorial, we’ll show you how to property or manage mail campaign information for efficient mass deletion. This helps you quickly stop unintended campaigns before they go to print, preventing early or incorrect mail from reaching customers.

Prerequisites

  • A large number of mail pieces needing to be canceled quickly

  • The mail pieces have not been sent to printers

Mass deletion using Lob metadata

const config: Configuration = new Configuration({
username: "test_XXXXXXXXX",
});
 
const postCardExample: PostcardEditable = {
    to: {
      company: "HARRY ZHANG",
      address_line1: "210 KING STREET",
      address_city: "SAN FRANCISCO",
      address_state: "CA",
      address_zip: "94107",
      address_country: CountryExtended.Us,
    },
    from: {
      company: "LEORE AVIDAR",
      address_line1: "185 BERRY ST",
      address_line2: "SUITE 6100",
      address_city: "SAN FRANCISCO",
      address_state: "CA",
      address_zip: "94107",
      address_country: CountryExtended.Us,
    },
    front = "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
    back = "<html style='padding: 1in; font-size: 20;'>Back HTML</html>",
    metadata: {
      batch_id: "NEWYORK2022"
   }
 };
const postcardsApi = new PostcardsApi(config);
const postcard = await postcardsApi.create(postCardExample); 
import lob
lob.api_key = "test_XXXXXXXXX"
 
postcard = lob.Postcard.create(
   to_address = {
     "name": "HARRY ZHANG",
     "address_line1": "210 KING STREET",
     "address_city": "SAN FRANCISCO",
     "address_state": "CA",
     "address_zip": "94107"
   },
   from_address = {
     "name": "LEORE AVIDAR",
     "address_line1":  "185 BERRY STREET",
     "address_line2": "SUITE 6100",
     "address_city": "SAN FRANCISCO",
     "address_state": "CA",
     "address_zip": "94107",
     "address_country": "US"
     },
   front = "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
   back = "<html style='padding: 1in; font-size: 20;'>Back HTML</html>",
   metadata = {
        "batch_id": "NEWYORK2022"
   }
)
require 'lob.rb'
require 'pp'
 
lob = Lob::Client.new(api_key: "test_XXXXXX")
 
newPostcard = lob.postcards.create(
   to: {
     name: "HARRY ZHANG",
     address_line1: "210 KING STREET",
     address_city: "SAN FRANCISCO",
     address_state: "CA",
     address_zip: "94107"
   },
   from: {
     name: "LEORE AVIDAR",
     address_line1: "185 BERRY STREET",
     address_line2:  "SUITE 6100",
     address_city: "SAN FRANCISCO",
     address_state: "CA",
     address_zip: "94107"
   },
   front: "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
   back: "<html style='padding: 1in; font-size: 20;'>Back HTML</html>",
   metadata: {
     "batch_id": "NEWYORK2022"
   }
 )
$lob = new \Lob\Lob('test_XXXXXX');
 
$postcard = $lob->postcards()->create(array(
    "to[name]" => "HARRY ZHANG",
    "to[address_line1]" => "210 KING STREET",
    "to[address_city]" => "SAN FRANCISCO",
    "to[address_state]" => "CA",
    "to[address_zip]" => "94107",
    "from[name]"            => "LEORE AVIDAR",
    "from[address_line1]" => "185 BERRY STREET",
    "from[address_line2]" => "SUITE 6100",
    "from[address_city]" => "SAN FRANCISCO",
    "from[address_state]" => "CA",
    "from[address_zip]" => "94107",
    "front" => "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
    "back" => "<html style='padding: 1in; font-size: 20;'>Back HTML</html>",
     "metadata[batch_id]" => "NEWYORK2022"
));
import java.util.Map;
import com.lob.Lob;
import com.lob.model.Address;
import com.lob.model.Postcard;
import com.lob.net.LobResponse;
 
public class App 
{
  public static void main( String[] args )
  {
     Lob.init("test_XXXXXXXX");
 
     Map<String, String> metaData = Map.of(
       "batch_id", "NEWYORK2022"
     );
 
     try {
        LobResponse<Postcard> response = new Postcard.RequestBuilder()
         .setTo(
             new Address.RequestBuilder()
                 .setName("HARRY ZHANG")
                 .setLine1("210 KING STREET")
                 .setCity("SAN FRANCISCO")
                 .setState("CA")
                 .setZip("94107")
             )
         .setFrom(
             new Address.RequestBuilder()
                 .setName("LEORE AVIDAR")
                 .setLine1("210 KING STREET")
                 .setCity("SAN FRANCISCO")
                 .setState("CA")
                 .setZip("94107")
             )
         .setFront("<html style='padding: 1in; font-size: 50;'>Front HTML</html>")
         .setBack("<html style='padding: 1in; font-size: 20;'>Back HTML</html>")
         .setMetadata(metaData)
         .create();
 
         Postcard postcard = response.getResponseBody();
         System.out.println(postcard);
      } catch (Exception err) {
         System.out.println("Error on postcard creation: " + err);
      }
    }
 }

In order to delete all postcards from the NEWYORK2022 campaign, use the list function and pass a metadata object with a batch_id key set to NEWYORK2022. This returns a list of all NEWYORK2022 campaign postcards, loop over each one and pass the postcard id into the cancel function.

const config: Configuration = new Configuration({
username: "test_XXXXXXXXX",
});
const postcardsApi = new PostcardsApi(config);
const listOfPostcards = await postcardsApi.list(
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    { batch_id: "NEWYORK2022" }
);
 
 for (let postcard of listOfPostcards) {
     await postcardsApi.cancel(postcard.id);
}
import lob
lob.api_key = "test_XXXXXXXXXXXXXX"
 
 list_of_postcards = lob.Postcard.list(metadata={"batch_id": "NEWYORK2022"})
 
for i in list_of_postcards.data: 
   lob.Postcard.delete(i.id)
require 'lob.rb'
require 'pp'
require 'json'
 
lob = Lob::Client.new(api_key: "test_XXXXXXXXXX")
 
list = lob.postcards.list(metadata: {"batch_id": "NEWYORK2022"})
data = list['data']
 
data.each do |item|
   begin
      lob.postcards.destroy( item['id'])
   rescue Lob::InvalidRequestError => e
      obj = JSON.parse(e.json_body)
      pp obj['error']['message']
   end
 end
$metadata = ["batch_id" => "NEWYORK2022"];
$list = $lob->postcards()->all(array('metadata' => $metadata));
 
foreach($list['data'] as $item) {
    try {
        $lob->postcards()->delete($item['id']);
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
}
import java.util.Map;
import java.util.HashMap;
import com.lob.Lob;
import com.lob.model.PostcardCollection;
import com.lob.model.Postcard;
import com.lob.net.LobResponse;
 
public class App 
{
   public static void main( String[] args )
   {
     Lob.init("test_XXXXXXXXX");
 
     Map<String, String> metaData = Map.of(
         "batch_id", "NEWYORK2022"
     );
 
     Map<String, Object> params = new HashMap<>();
     params.put("metadata", metaData);
 
     try {
         LobResponse<PostcardCollection> listResp = Postcard.list(params);
         PostcardCollection list = listResp.getResponseBody();
 
         for(int i=0; i<list.getCount(); i++) {
           try {
               LobResponse<Postcard> deleteResponse = Postcard.delete(list.getData().get(i).getId());
           } catch (Exception err) {
               System.out.println("Error: " + err);
           }
         }            
     } catch (Exception err) {
         System.out.println("Error: " + err);
     }
   }
 }

Mass deletion utilizing your own infrastructure

If you plan to save details about individual mail pieces in your database, consider including an identifier for the campaign as well. In the example below, we create a new postcard and upon successful completion, we store details in our database.

Create a config using your LOB_API_KEY found in the lob dashboard.

For extra resiliency, you can pass in the batch id via metadata (see above example), as well as store it locally as seen below.

const config: Configuration = new Configuration({
username: "test_XXXXXXXXX",
});
const postCardExample: PostcardEditable = {
    to: {
      name: "HARRY ZHANG",
      address_line1: "210 KING STREET",
      address_city: "SAN FRANCISCO",
      address_state: "CA",
      address_zip: "94107",
      address_country: CountryExtended.Us,
    },
    from: {
      name: "LEORE AVIDAR",
      address_line1: "185 BERRY ST",
      address_line2: "SUITE 6100",
      address_city: "SAN FRANCISCO",
      address_state: "CA",
      address_zip: "94107",
      address_country: CountryExtended.Us,
    }
};
const postcardsApi = new PostcardsApi(config);
const postcard = await postcardsApi.create(postCardExample);
  
/* Save to your database with the postcard.id and a unique group id */ 
import lob
lob.api_key = "test_XXXXXXXXX"
 
postcard = lob.Postcard.create(
   to_address = {
     "name": "HARRY ZHANG",
     "address_line1": "210 KING STREET",
     "address_city": "SAN FRANCISCO",
     "address_state": "CA",
     "address_zip": "94107"
   },
   from_address = {
     "name": "LEORE AVIDAR",
     "address_line1":  "185 BERRY ST",
     "address_line2": "SUITE 6100",
     "address_city": "SAN FRANCISCO",
     "address_state": "CA",
     "address_zip": "94107",
     "address_country": "US"
     },
   front = "<html style='padding: 1in; font-size: 50;'>Front HTML for Postcard</html>",
   back = "<html style='padding: 1in; font-size: 20;'>Back HTML for Postcard</html>"
)
  
# Save to your database with the postcard.id and a unique group id 
require 'lob.rb'
require 'pp'
 
lob = Lob::Client.new(api_key: "test_XXXXXX")
 
newPostcard = lob.postcards.create(
   to: {
     name: "HARRY ZHANG",
     address_line1: "210 KING STREET",
     address_city: "SAN FRANCISCO",
     address_state: "CA",
     address_zip: "94107"
   },
   from: {
     name: "LEORE AVIDAR",
     address_line1: "185 BERRY ST",
     address_line2:  "SUITE 6100",
     address_city: "SAN FRANCISCO",
     address_state: "CA",
     address_zip: "94107"
   },
   front: "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
   back: "<html style='padding: 1in; font-size: 20;'>Back HTML</html>"
 )
//Save to your database with the postcard.id and a unique group id  
$lob = new \Lob\Lob('test_XXXXXX');
 
 $postcard = $lob->postcards()->create(array(
    "to[name]" => "HARRY ZHANG",
    "to[address_line1]" => "210 KING STREET",
    "to[address_city]" => "SAN FRANCISCO",
    "to[address_state]" => "CA",
    "to[address_zip]" => "94107",
    "from[name]"            => "LEORE AVIDAR",
    "from[address_line1]" => "185 BERRY STREET",
    "from[address_line2]" => "SUITE 6100",
    "from[address_city]" => "SAN FRANCISCO",
    "from[address_state]" => "CA",
    "from[address_zip]" => "94107",
    "front" => "<html style='padding: 1in; font-size: 50;'>Front HTML</html>",
    "back" => "<html style='padding: 1in; font-size: 20;'>Back HTML</html>"
));
 /* Save to your database with the postcard.id and a unique group id */ 
import java.util.Map;
import com.lob.Lob;
import com.lob.model.Address;
import com.lob.model.Postcard;
import com.lob.net.LobResponse;
 
public class App 
{
  public static void main( String[] args )
  {
     Lob.init("test_XXXXXXXX");
 
     try {
        LobResponse<Postcard> response = new Postcard.RequestBuilder()
         .setTo(
             new Address.RequestBuilder()
                 .setName("HARRY ZHANG")
                 .setLine1("210 KING STREET")
                 .setCity("SAN FRANCISCO")
                 .setState("CA")
                 .setZip("94107")
             )
         .setFrom(
             new Address.RequestBuilder()
                 .setName("LEORE AVIDAR")
                 .setLine1("210 KING STREET")
                 .setCity("SAN FRANCISCO")
                 .setState("CA")
                 .setZip("94107")
             )
         .setFront("<html style='padding: 1in; font-size: 50;'>Front HTML</html>")
         .setBack("<html style='padding: 1in; font-size: 20;'>Back HTML</html>")
         .create();
         /* Save to your database with the postcard.id and a unique group id */  
 
      } catch (Exception err) {
         System.out.println("Error on postcard creation: " + err);
      }
    }
 }

To mass delete, we fetch a list of mail pieces from our database using a unique id for our campaign and loop over all the mail pieces to cancel them.

Fetch the list ids from your database using your unique group id

const config: Configuration = new Configuration({
    username: LOB_API_KEY,
});
const postcardsApi = new PostcardsApi(config); 
 
for (let item of dbdata) {
 await postcardsApi.cancel(item.id);
 
     /* Remove the record from your database */ 
 }

Fetch the list ids from your database using your unique group id

import lob
lob.api_key = "test_XXXXXXXXXX"
 
for i in dbdata: 
 lob.Postcard.delete(i.id)
    /* Remove the record from your database */ 

Fetch the list ids from your database using your unique group id

dbdata.each do | item |
   begin
     lob.postcards.destroy( item['id'])
      # Remove the record from your database  
   rescue Lob::InvalidRequestError => e
     obj = JSON.parse(e.json_body)
     pp obj['error']['message']
   end
 end

Fetch the list ids from your database using your unique group id

foreach($dbdata as $item) {
    try {
        $lob->postcards()->delete($item['id']);
        /* Remove the record from your database */ 
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
}

Fetch the list ids from your database using your unique group id

import java.util.Map;
import java.util.HashMap;
import com.lob.Lob;
import com.lob.model.PostcardCollection;
import com.lob.model.Postcard;
import com.lob.net.LobResponse;
 
public class App 
{
   public static void main( String[] args )
   {
     Lob.init("test_XXXXXXXXX");
 
     for(int i=0; i<list.getCount(); i++) {
       try {
           LobResponse<Postcard> deleteResponse = Postcard.delete(list.getData().get(i).getId());
            /* delete record from your database */ 
 
        } catch (Exception err) {
           System.out.println("Error: " + err);
       }
     } 
   }
 }

The recommended approach for identifying batches of mail sent through Lob is to use the with a unique key-value pair. Below is an example of how to create a single mail piece that includes metadata with a batch_id set to “NEWYORK2022”. The batch_id can be anything you want as long as it is unique to the batch, is a maximum length of 500 characters and is used for all mail pieces in the batch.

Create a config using your LOB_API_KEY found in the . Find more on idempotency keys .

To see the full list of acceptable parameters in the list, visit the .

💻
A Lob account and API key
metadata property
docs
Lob support
cancellation window
leverage Lob’s metadata
within your infrastructure
Lob dashboard
here