Mass deletion setup

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

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 leverage Lob’s metadata property or manage mail campaign information within your infrastructure 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

The recommended approach for identifying batches of mail sent through Lob is to use the metadata property 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 Lob dashboard. Find more on idempotency keys here.

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); 

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.

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

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);
}

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.

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 */ 

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 */ 
 }

Last updated

Was this helpful?