Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions roboflow/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,31 @@ def get_batch(self, batch_id: str) -> Dict:
raise RuntimeError(f"Failed to get batch {batch_id}: {response.text}")

return response.json()

def delete_images(self, image_ids: List[str]):
"""
Delete images from a project.

Args:
image_ids (List[str]): A list of image IDs to delete.

Example:
>>> import roboflow
>>> rf = roboflow.Roboflow(api_key="")
>>> project = rf.workspace().project("PROJECT_ID")
>>> project.delete_images(image_ids=["image_id_1", "image_id_2"])
"""
url = f"{API_URL}/{self.__workspace}/{self.__project_name}/images?api_key={self.__api_key}"

payload = {"images": image_ids}

response = requests.delete(url, headers={"Content-Type": "application/json"}, json=payload)

if response.status_code != 204:
try:
error_data = response.json()
if "error" in error_data:
raise RuntimeError(error_data["error"])
raise RuntimeError(response.text)
except ValueError:
raise RuntimeError(f"Failed to delete images: {response.text}")
43 changes: 43 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,49 @@ def test_get_batch_error(self):

self.assertEqual(str(context.exception), "Batch not found")

def test_delete_images_success(self):
image_ids = ["image1.jpg", "image2.jpg"]
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images?api_key={ROBOFLOW_API_KEY}"

responses.add(
responses.DELETE,
expected_url,
status=204,
match=[
json_params_matcher(
{
"images": image_ids,
}
)
],
)

self.project.delete_images(image_ids=image_ids)

def test_delete_images_error(self):
image_ids = ["image1.jpg", "image2.jpg"]
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images?api_key={ROBOFLOW_API_KEY}"
error_response = {"error": "Failed to delete images"}

responses.add(
responses.DELETE,
expected_url,
json=error_response,
status=400,
match=[
json_params_matcher(
{
"images": image_ids,
}
)
],
)

with self.assertRaises(RuntimeError) as context:
self.project.delete_images(image_ids=image_ids)

self.assertEqual(str(context.exception), "Failed to delete images")

def test_classification_dataset_upload(self):
from roboflow.util import folderparser

Expand Down