How Can We Help?
Pure API: Bulk Delete of Profile Photos using PythonPure API: Bulk Delete of Profile Photos using Python
Introduction
The goal of this Python script is to facilitate the bulk deletion of profile photos by utilizing a list of UUIDs provided in a CSV file with the Pure API. For each UUID, the script sends a PUT
request to the specified API person endpoint, effectively removing the associated photo. This script can be a useful companion tool to the Bulk Upload of Profile photos using Python with Pure API script, enabling seamless photo management and cleanup in scenarios where profile photos need to be removed in bulk.
Pure API examples disclaimer
Disclaimer
• Proceed at your own risk. Running this script/function means you will not blame the author if this breaks your data.
• This script/function is provided AS IS without warranty of any kind. It is recommended to run them in a test-staging environment to assess the outcome and understand their functionality.
• Elsevier will not support or review any code that uses or is based on these examples. Nor will Elsevier support it in any other way.
• This script has been tested with Pure 5.31 but might not work for future versions of Pure.
• This script is developed for Python version 3.11 and have not been tested with any other version of Python.
• Please make sure any Pure server you run this script against, has been backed up prior to running this script, to insure you can return to a working Pure if things fail
Requirements
- Pure Admin Account
- Valid API Key
- API Documentation
- Postman
- Python 3.x is installed.
-
script.py
(your Python script) and the necessary libraries must be installed:requests
csv
-
Expertise Level
Knowledge of Python is required. Familiarity with the requests
library and how to make API calls using Python will be helpful. For additional reference, you can consult "Python API Requests: Fundamental Coding Examples" to get a better understanding of how to make API calls and handle responses in Python.
How it works
-
CSV Input: The script expects a CSV file (
toDeletePhotosUUIDs.csv
) containing UUIDs. Each UUID should be in the first column, with no header row required. -
API Interaction: For each UUID, the script constructs a request to the API endpoint and sends a
PUT
request with an empty profile photo list, effectively removing the photo associated with the UUID. - Error Handling: If the request fails, the script will log the failure, including the status code and any returned error message.
- The Images folder (if used for reference or future extensions) must contain the corresponding image files that match the names listed in the CSV file.
Usage Instructions
- Place the
toDeletePhotosUUIDs.csv
file in the same directory as the script. - Update the
csv_file_path
variable with the correct file name if necessary. - Replace the
endpoint
variable with the actual API endpoint URL for your system. - Insert your real API key into the
headers
section in place of'yourApiKey'
. - Run the script in a test or staging environment to ensure it behaves as expected before using it in a production environment.
Important Notes
- Ensure that the UUIDs in the CSV are accurate and that the corresponding person records exist in the system.
- This script is designed for deleting photos, and its actions are irreversible once executed.
Sample CSV File Structure
The toDeletePhotosUUIDs.csv
file should contain one column:
-
uuid
: The UUID of the person whose profile will be updated.
Ensure that the CSV file does not contain a header, or if it does, it will be skipped by the script.
Example CSV:
uuid
933da504-1d3a-455f-8e74-da853dbeea61
4d0dd5c2-3bd1-4f65-a839-6bf8c9703bde
dfcad164-49e9-459a-9396-eb93adf2b131
90fe4e71-8c93-417e-8eb0-8f886b3335f2
1da376b8-21ac-4992-9638-6160324c450e
Python Script
Below is the complete Python script. You can copy it and modify the parameters as needed.
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 20:22:47 2025
@author: mlr
# DISCLAIMER:
# running this script/function means you will not blame the author if this breaks your data.
# This script/function is provided AS IS without warranty of any kind.
# There is no support for the script.
# This script has been tested with Pure 5.31 but might not work for future versions of Pure.
# This script is developed for Python version 3.11, and have not been tested with any other version of Python.
# Please make sure any Pure server you run this script against, has been backed up prior to running this script,
# to insure you can return to a working Pure if things fails.
"""
import csv
import requests
import os
# Define the relative path for the CSV file (same folder as the script)
csv_file_path = 'toDeletePhotosUUIDs.csv'
# API endpoint URLs
# Make sure to replace these with your actual API URLs
endpoint = "https://youPureUrl/ws/api/persons/" # Replace with your actual person update URL
# API headers
headers = {
'api-key': 'yourApiKey', # Replace with your actual API key
'Accept': 'application/json',
'Content-Type': 'application/json'
}
def delete_photo_by_uuid(uuid):
url = f"{endpoint}{uuid}"
data = '{"profilePhotos": []}' # Empty profile photos to "delete" them
response = requests.put(url, headers=headers, data=data)
if response.status_code == 200:
print(f"Successfully deleted photo for UUID: {uuid}")
else:
print(f"Failed to delete photo for UUID: {uuid}. Status code: {response.status_code}")
print(f"Response: {response.text}")
def process_csv_and_delete_photos(csv_file_path):
if not os.path.exists(csv_file_path):
print(f"CSV file not found: {csv_file_path}")
return
with open(csv_file_path, newline='') as csvfile:
reader = csv.reader(csvfile)
# Skip the header if there is one
next(reader, None) # Remove this line if your CSV doesn't have a header
for row in reader:
uuid = row[0].strip() # Assuming the UUID is in the first column
print(f"Processing UUID: {uuid}")
delete_photo_by_uuid(uuid)
# Call the function to process the CSV and delete photos
process_csv_and_delete_photos(csv_file_path)
Example Command for Running the Script
Once all files are in place, simply run the script:
python script.py
Additional Notes
Error Handling: The script provides error messages for missing files or failed API calls.
Debugging: Use print statements to log payloads and responses for troubleshooting.
Customization: Replace placeholders such as API URLs and keys with actual values.
Updated at February 14, 2025