Delete zendesk tickets

Hello, I need to delete many tickets on Zendesk.
Here are the endpoints :

Delete Ticket
DELETE /api/v2/tickets/{ticket_id}

Bulk Delete Tickets
DELETE /api/v2/tickets/destroy_many?ids={ids}
Accepts a comma-separated list of up to 100 ticket ids.

I do have a list of 7000 IDs that I would like to process in chunks of 100 because that’s the limit, if I use the second option. But I was just thinking if it existed an option where I can “load” somewhere my 700 IDs and requests would work in a loop for all the IDs.

Ive tried adding in the environment a variable with a json specifying all the IDs + a pre-request script, I see all chunks of ticket ids (69 chunks) on the console, but only the last chunk seems to be processed and deleted. Could you help me trouble shooting that issue or tell me if there is an easier way to do it ?

I’ve attached the json body of my pre-request script + image of environment + image of console after request

I can’t see any attachments.

There you go

// Retrieve the 'ticket_ids' from the environment
let allTickets = pm.environment.get("ticket_ids");

// Check if the environment variable exists
if (!allTickets) {
    throw new Error("The 'ticket_ids' variable is not set in the environment. Ensure it's correctly defined.");
}

// Parse the JSON string into an object
let ticketIds = JSON.parse(allTickets);

// Ensure that the ticket IDs are valid and are numbers
let validIds = ticketIds.ticket_ids.map(id => Number(id));

// Function to split the ticket IDs into chunks of 100
function chunkArray(array, chunkSize) {
    let result = [ ];
    for (let i = 0; i < array.length; i += chunkSize) {
        result.push(array.slice(i, i + chunkSize));
    }
    return result;
}

// Split the ticket IDs into packets of 100
let idChunks = chunkArray(validIds, 100);

// For each chunk of 100, make a request to delete the tickets
idChunks.forEach((chunk, index) => {
    let formattedIds = chunk.join(','); // Join the chunk into a comma-separated string

    // Set the chunk as a request variable to be used in the request URL
    pm.variables.set("ids", formattedIds);
    
    // Log the chunk to the console for debugging
    console.log(`Sending chunk ${index + 1}:`, formattedIds);
    
    // Set next request to make sure Postman sends each chunk sequentially
    if (index < idChunks.length - 1) {
        postman.setNextRequest("DELETE request name or ID");
    }
});

setNextRequest just sets the next request that will run after all of the code in your pre-request and post-response script has completed.

Which means that it will quickly loop through your forEach array and is why it only appears to be running the last set of data.

Instead of using the forEach array, you need to use a method available to array’s called shift(). This will retrieve the first element from that array and then delete that element from the array.

The following is a basic example.

const response = pm.response.json().data;
let ticket_ids = response.ticket_ids;
// let ticket_ids = pm.environment.get("ticket_ids");

if (typeof chunks === 'undefined' || chunks.length == 0) {

    const chunk = (a, size) =>
        Array.from(
            new Array(Math.ceil(a.length / size)),
            (_, i) => a.slice(i * size, i * size + size)
        );

    chunks = chunk(ticket_ids, 5); 
}

let currentChunk = chunks.shift();

let csl = currentChunk.join(',') // comma separated list

console.log(csl); // pm.variables.set("ids", csl);

if (chunks.length > 0) {
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

You need to change the first two lines so your ticket_id’s come from your environment variable. I’m using Postman Echo to mimic the process.

The IF statement checks to see if you have an existing array. It sets a global variable that is used to control the loop.

I’m only console logging the results instead but hopefully you can see the loop in action.

Based on the following data set, and splitting the array into chunks of 5.

{
    "ticket_ids": [145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161]
}

You can see that the loop run 4 times.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.