Recent searches
No recent searches
Deleting a lot of tickets from your Zendesk instance? Not a big deal!
Posted Mar 10, 2023
Deleting a lot of tickets from your Zendesk instance can be a daunting task, especially if you have thousands of tickets that need to be removed. Fortunately, using Python and the Zendesk API, you can automate this process and save yourself a lot of time and effort.
Here are some tips and tricks to help you delete a lot of tickets from your Zendesk instance using Python and the Zendesk API:
-
Use the Zendesk API to retrieve the list of tickets that you want to delete. You can use filters to narrow down the list of tickets based on certain criteria, such as their status, tags, or creation date.
-
Once you have retrieved the list of tickets, loop through it and delete each ticket using the Zendesk API. Make sure to use the bulk delete endpoint to delete multiple tickets at once, as this will save you a lot of time.
-
Keep track of the progress of the deletion process by printing out the number of tickets that have been deleted and the number of tickets that are still left to delete. This will help you monitor the process and ensure that everything is going smoothly.
-
If you encounter any errors or issues during the deletion process, use the error handling mechanisms in Python to handle them gracefully. For example, you could log the error messages to a file or send them to an email address for further analysis.
-
Finally, test your Python script on a small subset of tickets before running it on your entire Zendesk instance. This will help you identify any issues or unexpected behavior before you start deleting a large number of tickets.
In conclusion, using Python and the Zendesk API to delete a lot of tickets from your Zendesk instance can be a powerful way to automate a tedious and time-consuming task. By following these tips and tricks, you can ensure that the process goes smoothly and that your Zendesk instance stays clean and organized.
Here is the code I used:
import requests import time import base64 # Replace with your Zendesk subdomain and API credentials SUBDOMAIN = 'HERE' #add subdomain here EMAIL = 'user@domain.com' #add email here api key here API_KEY = '' #add api key here # Read the ticket IDs from a text file, one ID per line with open('ticket_ids.txt', 'r') as f: ticket_ids = f.read().splitlines() # Set the maximum number of tickets to delete per API request MAX_TICKETS_PER_REQUEST = 99 # Split the ticket IDs into batches of maximum size MAX_TICKETS_PER_REQUEST ticket_id_batches = [ticket_ids[i:i+MAX_TICKETS_PER_REQUEST] for i in range(0, len(ticket_ids), MAX_TICKETS_PER_REQUEST)] # Set up the API request headers with Basic authentication headers = { 'Content-Type': 'application/json', 'Authorization': f'Basic {base64.b64encode(f"{EMAIL}/token:{API_KEY}".encode("utf-8")).decode("ascii")}', } total_deleted_tickets = 0 last_successful_ticket_id = None # Loop over each ticket ID batch and delete the tickets for ticket_ids_batch in ticket_id_batches: # Construct the URL to delete the tickets url = f'https://{SUBDOMAIN}.zendesk.com/api/v2/tickets/destroy_many.json?ids={",".join(ticket_ids_batch)}' # Send the API request to delete the tickets with timeout of 60 seconds response = requests.delete(url, headers=headers, timeout=60) # Check if the request was successful if response.status_code == 200: num_deleted_tickets = len(ticket_ids_batch) total_deleted_tickets += num_deleted_tickets last_successful_ticket_id = ticket_ids_batch[-1] print(f'{num_deleted_tickets} tickets deleted successfully. Total deleted: {total_deleted_tickets}.') else: error_ticket_id = None if len(ticket_ids_batch) == 1: error_ticket_id = ticket_ids_batch[0] else: # Find the first ticket ID in the batch that was not deleted successfully response_json = response.json() for ticket_id in ticket_ids_batch: if str(ticket_id) not in response_json['results']: error_ticket_id = ticket_id break print(f'Error deleting tickets. Last successful ticket ID: {last_successful_ticket_id}. Error ticket ID: {error_ticket_id}. Response: {response.text}') break # Print a loader to indicate that the code is still running for i in range(5): print('.', end='', flush=True) time.sleep(1) print() # Wait for 10 seconds before sending the next request time.sleep(10)
I added a new variable last_successful_ticket_id to keep track of the ID of the last ticket that was deleted successfully. If an error occurs during the deletion of a batch of tickets, the code will print the ID of the last successful ticket and the ID of the ticket where the error occurred.
Note that if a batch contains only one ticket and an error occurs, the code will assume that the error occurred with that ticket. Otherwise, it will check the API response to determine which ticket in the batch was not deleted successfully. If an error occurs, the code will break out of the loop and stop deleting tickets, so you will need to manually modify the list of ticket IDs to start from the last successful ticket ID and run the code again.
You can use it as is for our own purposes.
5
1 comment
Francesca Roig
Can you help me out? How would you go about retrieving IDs of all tickets with a specific tag OR within a specific view?
0