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 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.
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.
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.
constconfig:Configuration=newConfiguration({username:"test_XXXXXXXXX",});constpostcardsApi=newPostcardsApi(config);constlistOfPostcards=awaitpostcardsApi.list(undefined,undefined,undefined,undefined,undefined, { batch_id:"NEWYORK2022" });for (let postcard of listOfPostcards) {awaitpostcardsApi.cancel(postcard.id);}
import loblob.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)
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.
constconfig:Configuration=newConfiguration({username:"test_XXXXXXXXX",});constpostCardExample: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, }};constpostcardsApi=newPostcardsApi(config);constpostcard=awaitpostcardsApi.create(postCardExample);/* Save to your database with the postcard.id and a unique group id */
import loblob.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 */
importjava.util.Map;importcom.lob.Lob;importcom.lob.model.Address;importcom.lob.model.Postcard;importcom.lob.net.LobResponse;publicclassApp{publicstaticvoidmain( 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
constconfig:Configuration=newConfiguration({ username:LOB_API_KEY,});constpostcardsApi=newPostcardsApi(config); for (let item of dbdata) {awaitpostcardsApi.cancel(item.id);/* Remove the record from your database */ }
Fetch the list ids from your database using your unique group id
import loblob.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 rescueLob::InvalidRequestError=> e obj =JSON.parse(e.json_body) pp obj['error']['message']endend
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
importjava.util.Map;importjava.util.HashMap;importcom.lob.Lob;importcom.lob.model.PostcardCollection;importcom.lob.model.Postcard;importcom.lob.net.LobResponse;publicclassApp{publicstaticvoidmain( 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); } } } }