Zendesk Script – Delete all user tickets!

No Comments

Have you ever come across that moment where you want to delete all the tickets from a user but there are so many you don’t want to spend 10 minutes doing so?  Well, I have done a small script  that will allow you to delete all tickets from an specific user. Remember that you can always get the latest code in my GitHub profile .

This is the code:

var v_subdomain = "SUBDOMAIN";
var v_user_id = USERID;
var v_tickets_amount = AMOUNTOFTICKETS;

function getTickets(page) {

    $.ajax({
        url: "https://"+v_subdomain+".zendesk.com/api/v2/users/"+v_user_id+"/tickets/requested.json?page=" + page,
        type: "GET",
        dataType: "JSON"
    }).done(function(data) {
        var ticketIDs = data.tickets[0].id + "";
        for (var i = 1; i < data.tickets.length; i++) {
            ticketIDs += "," + data.tickets[i].id;
        }
        deleteTickets(ticketIDs);
    });
}


function deleteTickets(ticketIDs) {

    $.ajax({
        url: "https://"+v_subdomain+".zendesk.com/api/v2/tickets/destroy_many.json?ids=" + ticketIDs,
        type: "DELETE",
        dataType: "JSON"
    }).done(function(data) {
        console.log("Page Deleted");
    }).fail(function(data) {
        console.log("Can't delete page with ticket ID's " + ticketIDs);
    });
}

for (var j = 0; j < Math.ceil(v_tickets_amount/100); j++) {
    getTickets(j + 1);
}

It’s quite simple and what you need to do in order to make it to work is edit 3 simple things.

//Replace v_subdomain with your subdomain. If your account is myaccount.zendesk.com just type myaccount. Leave the quotation marks!
var v_subdomain = "SUBDOMAIN";

/*
*Replace v_user_id with the user ID for whom you want to delete all tickets. Go to Admin -> Manage -> People and click on the user. You will see that the url looks like this:
*https://myaccount.zendesk.com/agent/users/590180702/requested_tickets
*The USERID would be 590180702
*/
var v_user_id = USERID;

// Change AMOUNTOFTICKETS for the amount of tickets that the user has. If he has 4893 just write 4893 there.
var v_tickets_amount = AMOUNTOFTICKETS;

There is no confirmation for the action you are going to do so please make sure that the User ID is the correct one or you may delete the tickets for another user and there is no way to revert this. I will try to add some other verifications to avoid data loss but for the moment being remember:

with-great-power-comes-great-responsibility-and-great-drama