Skip to content

Instantly share code, notes, and snippets.

@fhoek
Last active January 15, 2019 17:54
Show Gist options
  • Save fhoek/52c08b16ddcd249be7737bce653d863c to your computer and use it in GitHub Desktop.
Save fhoek/52c08b16ddcd249be7737bce653d863c to your computer and use it in GitHub Desktop.
Permanently deletes tickets from zendesk using the API. There is also a multiple destroy API but this wasn't working for me. For that reason I created this rather "dumb" script.
#!/usr/bin/env python
#
# Copyright 2019 Trancon B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
__author__ = 'Frank van den Hoek'
__copyright__ = 'Copyright 2019, Trancon B.V.'
import requests
from requests.auth import HTTPBasicAuth
# arrange auth
company = ''
creds = {
'username' : '',
'password' : '',
}
basic_auth = HTTPBasicAuth(**creds)
# get deleted tickets
ticket_url = 'https://{company}.zendesk.com/api/v2/tickets/{id}.json'.format(company=company, id='{id}')
perm_del_url = 'https://{company}.zendesk.com/api/v2/deleted_tickets/{id}.json'.format(company=company, id='{id}')
for i in range(100): # adjust to number of tickets
response = requests.get(ticket_url.format(id=i), auth=basic_auth)
print "GET {} [status:{}]".format(i, response.status_code)
if response.status_code == 404:
continue
o = response.json()
ticket = None if 'ticket' not in o else o['ticket']
if not ticket:
continue
del_r = requests.delete(ticket_url.format(id=i), auth=basic_auth)
perm_del_r = requests.delete(perm_del_url.format(id=ticket['id']), auth=basic_auth)
print "DELETE {} [status:{}]".format(ticket['id'], perm_del_r.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment