Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aviau/d4c6670d53c8d763213303d7f7da345c to your computer and use it in GitHub Desktop.
Save aviau/d4c6670d53c8d763213303d7f7da345c to your computer and use it in GitHub Desktop.
Zendesk cleanup. Iterates over organisations and retrieves the number of tickets, if zero it will delete the org and its users
#!/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 the Zenpy Class
from zenpy import Zenpy
# An API token
creds = {
'email' : '',
'token' : '',
'subdomain': ''
}
zenpy_client = Zenpy(**creds)
to_delete = []
for org in zenpy_client.organizations():
ticket_count = zenpy_client.search(organization_id=org.id, type='ticket').count
if ticket_count == 0:
print 'Elected', org.name
to_delete.append(org)
for org in to_delete:
print u'Deleting [{}] {}...'.format(org.id, org.name)
users = zenpy_client.search(organization_id=org.id, type='user')
for user in users:
print u'\tDeleting user {}...'.format(user.name)
zenpy_client.users.delete(user)
print '\tDone'
result = zenpy_client.organizations.delete(org)
print 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment