-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdestroy_all_dvobjects.py
51 lines (47 loc) · 1.63 KB
/
destroy_all_dvobjects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pyDataverse.api import Api
import json
import dvconfig
import requests
import os
base_url = dvconfig.base_url
api_token = dvconfig.api_token
try:
api_token=os.environ['API_TOKEN']
print("Using API token from $API_TOKEN.")
except:
print("Using API token from config file.")
api = Api(base_url, api_token)
print('API status: ' +api.status)
dataverse_ids = []
dataset_ids = []
def main():
print("Finding dataverses and datasets to destroy...")
find_children(1)
dataset_ids.sort(reverse=True)
for dataset_id in dataset_ids:
print('Deleting dataset id ' + str(dataset_id))
print('Preemptively deleting dataset locks for dataset id ' + str(dataset_id))
resp = requests.delete(base_url + '/api/datasets/' + str(dataset_id) + '/locks?key=' + api_token)
print(resp)
resp = requests.delete(base_url + '/api/datasets/' + str(dataset_id) + '/destroy?key=' + api_token)
print(resp)
dataverse_ids.sort(reverse=True)
for dataverse_id in dataverse_ids:
print('Deleting dataverse id ' + str(dataverse_id))
resp = api.delete_dataverse(dataverse_id)
print(resp)
print("Done.")
def find_children(dataverse_database_id):
query_str = '/dataverses/' + str(dataverse_database_id) + '/contents'
params = {}
resp = api.get_request(query_str, params=params, auth=True)
for dvobject in resp.json()['data']:
dvtype = dvobject['type']
dvid = dvobject['id']
if 'dataverse' == dvtype:
find_children(dvid)
dataverse_ids.append(dvid)
else:
dataset_ids.append(dvid)
if __name__ == '__main__':
main()