API python script to delete multiple projects

Mariano Edvardsson February 15, 2024

Hello! I'm trying to delete multiple projects with the following Python script:

 

import requests
from requests.auth import HTTPBasicAuth

domain = "https://MY_JIRA_INSTANCE.atlassian.net"
admin_user = "JIRA_ADMIN@MY_COMPANY.com"
admin_token = "JIRA_ADMIN_TOKEN"
project_query = "PROJECTS_I_WANT_TO_DELETE"

def getJson(url, auth):
r = requests.request(
"GET",
url,
headers={"Accept": "application/json"},
auth=auth
)
return r.json()

def deleteProject(id, auth):
r = requests.request(
"DELETE",
domain + "/rest/api/3/project/" + id,
auth=auth
)
return r.text

search_url = domain + "/rest/api/3/project/search?query=" + project_query
auth = HTTPBasicAuth(admin_user, admin_token)
json = getJson(search_url, auth)

projectIds = []

# append results across all pages, while there's still a nextPage, to projectIds array
while "nextPage" in json:
nextUrl = json["nextPage"]
for searchResult in json['values']:
# optional safety check to make sure the right project is being added to the deletion array
# if "PROJECT_NAME_I_INTEND_TO_DELETE" in searchResult["name"]:
projectIds.append(searchResult['id'])
print("Number of project IDs found matching the search query: " + str(len(projectIds)))
json = getJson(nextUrl, auth)

# append a single page, or the last page of results, to projectIds array
for searchResult in json['values']:
# optional safety check to make sure the right project is being added to the deletion array
# if "PROJECT_NAME_I_INTEND_TO_DELETE" in searchResult["name"]:
projectIds.append(searchResult['id'])
print("Number of project IDs found matching the search query: " + str(len(projectIds)))

# delete projects in projectIds array
for index, id in enumerate(projectIds):
print("Deleting project " + id + ". Projects remaining: " + str(len(projectIds)-index))
print(deleteProject(id, auth))

 

However I'm encountering issues when trying to delete multiple projects.

I take it JQL does not support RegEx, but how do I do to delete more than one project? I guess that the script does not accept tuples because it really works with JQL, but I'm having trouble creating a project query that I can simply put in a few project names (e.g: CUST, SUP, PJ01, etc). I've tried going "CUST" or "SUP", etc but that did not work, I only get one project deleted.

 

Any help would be greatly appreciated!

 

1 answer

0 votes
Dexter de Vera
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2024

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events