CKAN: delete a list of records within the DataStore in a single query

459 views Asked by At

NOTE: My original problem is to delete all the records except for the last N ones, but guessing such a thing is impossible with CKAN API, I've come up with the following solution:

  1. To get the total ammount of records using datastore_search.
  2. To get the ID of the first record in the previous search.
  3. To obtain the IDs of all the records from the first record ID to total-N.

So, my question is: how to delete all the identified IDs in a single query? I've tried the following:

$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":{"_id":1,"_id":2,"_id":3},"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"
{"help": "http://demo.ckan.org/api/3/action/help_show?name=datastore_delete", "success": true, "result": {"filters": {"_id": 3}, "resource_id": "a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}}

But only the record regarding the last ID (3) was removed.

Of course, an array did not work as well:

$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":[{"_id":1},{"_id":2},{"_id":3}],"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"
{"help": "http://demo.ckan.org/api/3/action/help_show?name=datastore_delete", "success": false, "error": {"__type": "Validation Error", "filters": ["filters must be either a dict or null."]}}

Any idea? Or should I perform a query for each record aimed to be deleted?

1

There are 1 answers

0
frb On BEST ANSWER

Auto-answering. After trying several combinations, I've found this works:

$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":{"_id":[1,2]},"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"

I.e. a dictionary has to be sent with a single (key,value) pair, the _id and an array of values for the _id, respectively.