QLands
QLands

Reputation: 2586

Delete and purge a package in CKAN using API?

Using CKAN API I can delete a package (which marks it as inactive). But how can I purge it (delete it from the database) using API and NOT the admin interface?

Upvotes: 4

Views: 1748

Answers (2)

user3366016
user3366016

Reputation: 1312

Just updating for newer versions of CKAN.

You can purge directly with the API now.

https://docs.ckan.org/en/2.8/api/#ckan.logic.action.delete.dataset_purge

import requests

api_token = 'Xxxx-XXXXX-xxx-xxx'
endpoint = 'http://<url>/api/3/action/dataset_purge'
headers = {'Content-type': 'application/json',
           'Authorization': api_token}

payload = {'id': 'id or name of dataset'}

res = requests.post(endpoint,
                    json=payload,
                    headers=headers)

print(res.status_code, res.reason)

You can also checkout the ckanapi bulk delete actions.

Upvotes: 1

jaybrau
jaybrau

Reputation: 413

The CKAN API doesn't currently support purging trash, but you can automate the admin trash purge form submission with a POST to /ckan-admin/trash with a payload of "purge-packages=purge", as an admin. Here's how in Python:

import urllib2

admin_api_key = '<my_admin_api_key>'
ckan_base = 'http://my_site.net'

request = urllib2.Request('{0}/ckan-admin/trash'.format(ckan_base))
request.add_header('Authorization', admin_api_key)
response = urllib2.urlopen(request, 'purge-packages=purge')
assert response.code == 200

Upvotes: 4

Related Questions