How to replace csv media file or delete existing media file using kobo v2 API

Hello to everyone. My name is Uche, and I’m a newcomer to the community. I’ve been using ODK for a while now, but I recently decided to switch to Kobotoolbox for a nationwide survey tool I’m working on.
I’m currently having issues replacing a csv media file using the Kobo v2 API. My python script successfully uploads fresh csv media files but fails to upload a file if the name already exists as a media file on the kobo platform. Is it possible to use the Kobo v2 API to replace an existing csv file with a new one with thesame name? and if not can I use the API to delete an existing media file?

Welcome to the community, @uchechinedu01! Could you also share your script so that we could also test and troubleshoot?

Hello @Kal_Lam. Thanks for reaching out. Please find below the python script used to upload a media file. The script successfully uploads a csv file if it does not exist on the kobo server but fails to upload (404 error) if the filename already exists. I want to be able to replace the csv file since the filename is hard-coded on the form.
The script:
import requests, json

KC_URL= “https://kobo.humanitarianresponse.info/api/v2/assets/aRhFBbWKF946rfCsXWA3Yj/files.json

TOKEN = ‘6ace07700f913915842402946d8cb01d49ad5be5’
XFORM = ‘aRhFBbWKF946rfCsXWA3Yj’

FILENAME = ‘input_file.csv’
MIME = ‘text/csv’

HEADERS = {‘Authorization’: f’Token {TOKEN}'}
payload = {‘filename’: ‘input_file.csv’}
files = {‘content’: open(‘input_file.csv’, ‘rb’)}
data = {‘description’: ‘Input file’, ‘metadata’: json.dumps(payload), ‘file_type’: ‘form_media’}

res = requests.post(url=KC_URL, headers=HEADERS, data=data, files=files)
print(res)
print(res.request.url)
print(res.request.headers)