Skip to content
Open
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
32 changes: 30 additions & 2 deletions operator-tools/README_collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,31 @@ uv run operator-tools/manage_collections.py batch-create stac/ --pattern "*-stag
- Reports success/failure for each file
- Summary statistics at the end

#### 4. `info` - Show Collection Information
#### 4. `delete` - Delete a Collection

Delete a collection from the STAC catalog. Some STAC servers require the collection to be empty before deletion.

```bash
# Delete a collection (will prompt for confirmation)
uv run operator-tools/manage_collections.py delete sentinel-2-l2a-staging

# Clean items first, then delete
uv run operator-tools/manage_collections.py delete sentinel-2-l2a-staging --clean-first

# Skip confirmation prompt
uv run operator-tools/manage_collections.py delete sentinel-2-l2a-staging --clean-first -y
```

**Options:**
- `--clean-first`: Remove all items from the collection before deleting it
- `--yes, -y`: Skip confirmation prompt

**Safety Features:**
- Confirmation prompt before deletion (unless `--yes` is used)
- Option to automatically clean items first
- Handles already-deleted collections gracefully

#### 5. `info` - Show Collection Information

Display detailed information about a collection, including item count.

Expand Down Expand Up @@ -178,10 +202,11 @@ uv run operator-tools/manage_collections.py batch-create stac/

The tool uses the following STAC Transaction API endpoints:

- `GET /collections/{collection_id}/items` - List items (for cleaning)
- `GET /collections/{collection_id}/items` - List items (for cleaning and info)
- `DELETE /collections/{collection_id}/items/{item_id}` - Delete item
- `POST /collections` - Create collection
- `PUT /collections` - Update collection
- `DELETE /collections/{collection_id}` - Delete collection

## Error Handling

Expand Down Expand Up @@ -243,6 +268,9 @@ uv run operator-tools/manage_collections.py clean sentinel-2-l2a-staging -y

# 4. Update collection metadata
uv run operator-tools/manage_collections.py create stac/sentinel-2-l2a.json --update

# 5. (If needed) Delete collection
uv run operator-tools/manage_collections.py delete sentinel-2-l2a-staging --clean-first -y
```

### Development Workflow
Expand Down
83 changes: 83 additions & 0 deletions operator-tools/manage_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Supports:
- Cleaning collections (removing all items)
- Creating/updating collections from templates
- Deleting collections
"""

import json
Expand Down Expand Up @@ -131,6 +132,36 @@ def clean_collection(self, collection_id: str, dry_run: bool = False) -> int:
)
return deleted_count

def delete_collection(self, collection_id: str) -> bool:
"""
Delete a collection using Transaction API.

Args:
collection_id: ID of the collection to delete

Returns:
True if successful, False otherwise
"""
url = f"{self.api_url}/collections/{collection_id}"

try:
response = self.session.delete(url, timeout=30)
response.raise_for_status()
click.echo(f"✅ Collection {collection_id} deleted successfully")
return True
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
click.echo(f"⚠️ Collection {collection_id} not found", err=True)
return False
click.echo(
f"❌ Failed to delete collection {collection_id}: {e.response.status_code} - {e.response.text}",
err=True,
)
return False
except Exception as e:
click.echo(f"❌ Error deleting collection {collection_id}: {e}", err=True)
return False

def create_or_update_collection(
self, collection_data: dict[str, Any], update: bool = False
) -> bool:
Expand Down Expand Up @@ -368,6 +399,58 @@ def batch_create(ctx: click.Context, directory: Path, update: bool, pattern: str
click.echo(f"❌ Failed: {failed_count}")


@cli.command()
@click.argument("collection_id")
@click.option(
"--clean-first",
is_flag=True,
help="Remove all items from the collection before deleting it",
)
@click.option(
"--yes",
"-y",
is_flag=True,
help="Skip confirmation prompt",
)
@click.pass_context
def delete(ctx: click.Context, collection_id: str, clean_first: bool, yes: bool) -> None:
"""
Delete a collection.

Note: Some STAC servers require the collection to be empty before deletion.
Use --clean-first to automatically remove all items first.

Examples:
manage_collections.py delete sentinel-2-l2a-staging
manage_collections.py delete sentinel-2-l2a-staging --clean-first
manage_collections.py delete sentinel-2-l2a-staging --clean-first --yes
"""
manager: STACCollectionManager = ctx.obj["manager"]

if not yes:
click.confirm(
f"⚠️ This will permanently delete collection '{collection_id}'. Continue?",
abort=True,
)

try:
# Clean collection first if requested
if clean_first:
click.echo("\n📋 Cleaning collection before deletion...")
manager.clean_collection(collection_id, dry_run=False)

# Delete the collection
click.echo(f"\n🗑️ Deleting collection: {collection_id}")
success = manager.delete_collection(collection_id)

if not success:
raise click.Abort()

except Exception as e:
click.echo(f"❌ Operation failed: {e}", err=True)
raise click.Abort() from e


@cli.command()
@click.argument("collection_id")
@click.pass_context
Expand Down
Loading