Delete submissions using rest api

Hi,

I would like to know if it is possible via an API call to delete specific submissions based on the submission_date (today) for example? My form will remain in place for a long time and I would like to purge the data regularly.

Thanks for any insight
Patrick

Hi
I am not familiar with this but @Xiphware may be able to assist

Regards
Stephane

Yes. You use the REST “DELETE” method against desired submission endpoint

AFAIK there is no exposed REST API to delete a one or more submissions based on a specific criteria, eg date.

Instead, what you have to do is identify the unique REST endpoint corresponding to the specific submission instance under its associated form instance. Typically, the process would be to enumerate your forms (if you didnt already know the form id), eg

HOST=https://kf.kobotoolbox.org
curl -v --header 'Accept: application/json' --user tiritea:$PASSWORD $HOST/assets/

which will give you a long list of all your forms. From this you need to identify the form associated with the submission you wish to delete (FYI this is the same the id used when you save your form), eg

...
"url": "https://kf.kobotoolbox.org/assets/aKydtLQwpyH4XdFfmB2xTB/"
...

where the form id in my case is “aKydtLQwpyH4XdFfmB2xTB”. Then you can list all the submissions for this form using another REST call:

ID=aKydtLQwpyH4XdFfmB2xTB
curl -v --header 'Accept: application/json' --user tiritea:$PASSWORD $HOST/assets/$ID/submissions/

which will give you a (long?) list of all the submissions back, of the form

{
  "_notes": [

  ],
  "this_is_a_test": "hello world",
  "meta/instanceID": "uuid:61059adb-c644-4f73-8303-3c56950df9b2",
  "end": "2019-08-02T12:59:33.923+12:00",
  "_submission_time": "2019-08-02T00:59:34",
  "_validation_status": {
  },
  "_uuid": "61059adb-c644-4f73-8303-3c56950df9b2",
  "_bamboo_dataset_id": "",
  "_tags": [

  ],
  "_attachments": [

  ],
  "start": "2019-08-02T12:59:27.428+12:00",
  "_submitted_by": null,
  "_geolocation": [
    null,
    null
  ],
  "_xform_id_string": "aKydtLQwpyH4XdFfmB2xTB",
  "_status": "submitted_via_web",
  "_id": 33920455,
  "__version__": "vFMDEBaPxSixQVvYbk9uwh",
  "formhub/uuid": "f8fc6dbda2554447ba3e9711de5a9718"
}

If you want you can filter this list by date, or what-have-you, to decide which submission(s) you want to delete. The thing you are looking for is the unique submission id, eg above:

"_id": 33920455,

Once you have this, you can perform a REST “DELETE” against that specific submission REST endpoint

curl -X DELETE -v --user tiritea:$PASSWORD $HOST/assets/$ID/submissions/33920455/
...
< HTTP/2 204 

which should return an HTTP status code of 204 (No Content) which is typically used to indicate the associated resource was successfully deleted. You can confirm by trying to GET it again

curl -X GET -v --user tiritea:$PASSWORD $HOST/assets/$ID/submissions/33920455/
...
< HTTP/2 404 
...
{
  "detail": "Not found."
}

which should return an HTTP 404 (No Found)

Basically, to delete a submission:

  1. GET list all forms using $HOST/assets/
  2. determine the target formid (if you dont already know it)
  3. GET list the form’s submissions: $HOST/assets/{formid}/submissions/
  4. determine the target submission id
  5. DELETE the submission: $HOST/assets/{formid}/submissions/{submissionid}/

If you go back into the Kobo console you should find that submission is gone.

3 Likes

Not deleting tried this API :$HOST/assets/{formid}/submissions/{submissionid}/

But my host is “https://kc.humanitarianresponse.info/

How to delete a submission with this host??

Try changing this (from above):

HOST=https://kf.kobotoolbox.org

to this:

HOST=https://kc.humanitarianresponse.info

$HOST is how you specify shell variables for command-line.

1 Like

Actually , I don’t know how to use this code in Goolge Apps Script . Still stuck there…

Thank you for your reply …

The other options didnt work for me, but after alot of research I finally found the solution which worked for me. You can use python to send a request.delete() as follows

jdata = {
“payload”: {“submission_ids”:[“99991”,“99992”]}
}
eurl = “https://HOST/api/v2/assets/AssetID/data/bulk/
resp = requests.delete(url=eurl, headers=h,data =json.dumps(jdata))

Thanks!

1 Like