Deepbits Platform: Delete a file
This script demonstrates how to delete files from the Deepbits platform using the DELETE /sbom_builder/{detail_id} API endpoint. Files can be deleted by their ID (detail_id) or by their name (file_name). If a file is linked to at least one project, the deletion operation will fail. However, it provides an option to force deletion (force=True) to override this constraint. The file will be unlinked from all associated projects.
Delete File by ID. The delete_file function deletes a file from the Deepbits platform by specifying the file's ID. It optionally allows forcing deletion (force=True) to bypass restrictions.
Delete Files by Name. The delete_files_by_name function retrieves a list of file IDs associated with a specified file name (file_name) and deletes each file. It supports force deletion to ensure removal even if the files are in use.
Below is a demo script demonstrating how to delete files by ID (FILE_ID) and by name (FILE_NAME). Adjust the placeholders , , and with your specific values for integration.
import requests
API_KEY = '<Your API key generated from app.deepbits.com>'
API_BASE = 'https://api.deepbits.com/api/v1'
default_headers = {"x-api-key": API_KEY }
FILE_ID = '<Your File ID>'
FILE_NAME = '<Your file name>'
def main():
# delete a file by id
response = delete_file(FILE_ID)
print(response)
response = delete_file(FILE_ID, force=True)
print(response)
# delete files by a file name
delete_files_by_name(FILE_NAME)
delete_files_by_name(FILE_NAME, force=True)
def delete_files_by_name(file_name, force=False):
file_id_list = get_file_id_list_by_name(file_name)
print('file_id_list:', file_id_list)
for file_id in file_id_list:
print('try to delete file:', file_id)
response = delete_file(file_id, force=force)
print(response)
def get_file_id_list_by_name(file_name):
try:
url = f"{API_BASE}/sbom_builders"
headers = {
"accept": "application/json",
"x-api-key": API_KEY
}
file_id_list = []
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json().get('data', [])
for item in data:
if item.get('file_name') == file_name:
file_id_list.append(item.get('_id'))
return file_id_list
except Exception as e:
print(f"An unexpected error occurred: {e}")
print(response.text)
return []
def delete_file(detail_id, force=False):
try:
url = f"{API_BASE}/sbom_builder/{detail_id}"
headers = {
"accept": "application/json",
"x-api-key": API_KEY
}
params = {
'force': 'true' if force else 'false'
}
response = requests.delete(url, headers=headers, params=params)
response.raise_for_status() # Raise an exception for HTTP errors
return response.text
except Exception as e:
print(f"An unexpected error occurred: {e}")
print(response.text)
return None
if __name__ == "__main__":
main()
Updated 4 months ago