We’ve migrated our documentation to a new site, which means some URLs have changed.
Audience

Audience: How to delete user profile data?

This page shows a Python script that deletes the user profile data for a specific bucket/prefix.

Note: This will not delete the user IDs stored in this bucket.

Enter your username & API Key, as well as the prefix in question.

# coding: utf-8
# ## loading all libs
# In[16]:
import sys
import json
import os
from datetime import datetime as dt, timedelta, date
import time
import hashlib
import hmac
import http.client
import json
import string
import random
def cx_api(path, obj):
    username = "<username>"
    secret = "<pass>"
    # Locate and autoload configuration from ~/.cxrc
    rc = os.path.join(os.path.expanduser('~'), '.cxrc')
    if os.path.exists(rc):
        for line in open(rc):
            fields = line.split()
            if fields[0] == 'authentication' and len(fields) == 3:
                username = fields[1]
                secret = fields[2]
    date = dt.utcnow().isoformat() + "Z"
    signature = hmac.new(secret.encode('utf-8'), date.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
    headers = {"X-cXense-Authentication": "username=%s date=%s hmac-sha256-hex=%s" % (username, date, signature)}
    headers["Content-Type"] = "application/json; charset=utf-8"
    connection = http.client.HTTPSConnection("api-de.cxense.com", 443, timeout=600)
    connection.request("POST", path+"?admin=true", json.dumps(obj), headers)
    response = connection.getresponse()
    status = response.status
    responseObj = json.loads(response.read().decode('utf-8'))
    connection.close()
    return status, responseObj
 
payload = {"type":"<prefix>"}
result = cx_api("/profile/user/external/read", payload)
if "data" in result[1]:
    users = result[1]['data']
else:
    users = []
delete = []
count = 1
for u in users:
    if count % 100 == 0:
        cx_api("/profile/user/external/delete", delete)
        print(count)
        delete = []
    else:
        delete.append({"type":u['type'], "id":u['id']})
    count += 1
cx_api("/profile/user/external/delete", delete)
print(count)

Last updated: