How to delete a collection of IDs in POSTMAN

Today I had to delete several objects that I created when testing an API. It was very easy to click the Send button and create a bunch of stuff but now I have to clean the DB. I am using Postman as testing tool and I chose to execute the following code in the Pre-request Script of my test. The thing is that you are able to use Lodash to help you with all this messy things rather than doing it the old way with for loops and all that stuff (which I like BTW because old school is the base of modern programming).

Here is the code:

var baseUrl = 'https://www.example.com/api/store/products';

// Collection of target IDs to delete
var targetIds = [
    "efc2c85c-7e6b-47dc-ad3f-d39b1e39db94",
    "749baefb-4807-410d-8e0d-5af838a90c67",
    "01230409-9a5c-4997-96f6-268f3fcbe820",
    "f6570387-c29c-428f-8da1-0bf9c5634663",
    "8cff6d4f-f995-49f9-b61a-724439084635",
    "fa6b4f29-8ac1-4e12-9874-4bca1576e4c7",
    "f6246385-6354-4336-bccb-3516257f4f95",
    "a5b9f27f-90ea-47d4-ac72-f93f7ca26156",
    "21df8d53-6004-4367-aaac-fe13d12342e3",
    "853ba57f-9fca-47d0-b192-b4038bf3cfb9",
    "8bf58c9a-8674-4e9e-b0a0-3c14400dc6ea",
    "3ffd3b74-1a16-4d2c-9afc-987ef66294bc",
    "18ee9108-cf03-4083-9a39-6c448295531b",
    "f75d38a3-94fa-48e4-be30-89d81b486cad",
    "49275cd1-f25e-4f22-a318-339a39b17554",
    "451babb6-b948-46f0-9c6c-6a0f55f49d83",
    "13208e41-7e0a-417a-9639-00c9974dc18f",
    "4a0144b2-030c-4363-8f2e-23dc36c056d9",
    "91c6a6d4-46f9-4238-92ce-86c4a876932c",
    "b48074d5-d353-451e-98a9-5a09527b635e",
    "fc313892-5a4a-4fd2-bc90-f01e19452435"
];

// Iterating the collection
_.each( targetIds, function(targetId) {
    
    // Sending a DELETE request for every ID on the collection
    pm.sendRequest( {
        url: baseUrl + '/'+ targetId,
        method: 'DELETE',
    }, 
    function (err, res) {} );
});

Leave a Comment

Your email address will not be published. Required fields are marked *