Delete Operations
Learn how to programmatically delete files and ontologies from the Perseus platform and verify their removal.
The full code for this example is available on GitHub.
The Perseus SDK provides simple functions to manage the lifecycle of your data. This example demonstrates how to programmatically delete files and ontologies that you no longer need.
What You'll Learn
- How to upload and delete a file from the Perseus platform.
- How to verify that a file has been successfully deleted.
- How to perform the same upload, delete, and verify workflow for an ontology.
Example Use Case: Data Cleanup
Imagine you have a data processing pipeline where you temporarily upload files and ontologies for graph creation. Once the graphs are built and stored, it's good practice to clean up the source files to save space and keep your workspace tidy. This script shows the essential commands for automating that cleanup process.
Prerequisites
Before running this example, ensure you have followed the installation guide to set up your environment and obtain the necessary API keys.
Code Walkthrough
File Deletion
This section demonstrates a complete cycle: uploading a file, deleting it, and confirming its removal.
Uploading the File
First, we upload a sample file to have something to delete. The perseus_client.upload_file() function handles this and returns a File object containing the ID of the uploaded file.
# This code goes inside a try block
file_path = "assets/file_to_delete.txt"
print(f"Uploading file: {file_path}")
uploaded_file = perseus_client.upload_file(file_path)
print(f"File uploaded successfully. File ID: {uploaded_file.id}")Deleting the File
Using the ID from the uploaded file, we call perseus_client.delete_file(). This function sends a request to the Perseus API to remove the file.
print(f"Deleting file with ID: {uploaded_file.id}")
perseus_client.delete_file(uploaded_file.id)
print("Delete command issued successfully.")Verifying the Deletion
To ensure the deletion was successful, we can use perseus_client.find_files() and attempt to find the file by its ID. If the function returns an empty list, the file has been successfully removed.
print(f"Verifying that file {uploaded_file.id} has been deleted...")
found_files = perseus_client.find_files(ids=[uploaded_file.id])
if not found_files:
print(f"Verification successful: File {uploaded_file.id} was not found.")
else:
print(f"Verification failed: File {uploaded_file.id} still exists.")Ontology Deletion
The process for deleting an ontology is identical to deleting a file, but uses the corresponding ontology-specific functions.
Uploading the Ontology
First, we upload a sample ontology to have something to delete. The perseus_client.upload_ontology() function handles this and returns an Ontology object containing the ID of the uploaded ontology.
ontology_path = "assets/ontology_to_delete.ttl"
# Step 1: Upload an ontology
print(f"Uploading ontology: {ontology_path}")
uploaded_ontology = perseus_client.upload_ontology(ontology_path)Deleting the Ontology
Using the ID from the uploaded ontology, we call perseus_client.delete_ontology(). This function sends a request to the Perseus API to remove the ontology.
# Step 2: Delete the ontology
print(f"Deleting ontology with ID: {uploaded_ontology.id}")
perseus_client.delete_ontology(uploaded_ontology.id)Verify the Ontology Deletion
Similarly, we use find_ontologies() to confirm that the ontology has been removed.
print(f"Verifying that ontology {uploaded_ontology.id} has been deleted...")
found_ontologies = perseus_client.find_ontologies(
ids=[uploaded_ontology.id]
)
if not found_ontologies:
print(f"Verification successful: Ontology {uploaded_ontology.id} was not found.")
else:
print(f"Verification failed: Ontology {uploaded_ontology.id} still exists.")