|
| 1 | +""" |
| 2 | +Delete all Supermemory documents for one or more containerTags (sample_ids). |
| 3 | +
|
| 4 | +Usage: |
| 5 | + # Delete a single container |
| 6 | + python delete_container.py conv-26 |
| 7 | +
|
| 8 | + # Delete multiple containers |
| 9 | + python delete_container.py conv-26 conv-31 conv-45 |
| 10 | +
|
| 11 | + # Delete first N samples from locomo10.json |
| 12 | + python delete_container.py --from-data --limit 2 |
| 13 | +
|
| 14 | + # Delete all samples from locomo10.json |
| 15 | + python delete_container.py --from-data |
| 16 | +""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import os |
| 21 | +import re |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +from dotenv import load_dotenv |
| 26 | + |
| 27 | +load_dotenv(Path.home() / ".openviking_benchmark_env") |
| 28 | + |
| 29 | +try: |
| 30 | + from supermemory import Supermemory |
| 31 | +except ImportError: |
| 32 | + print("Error: supermemory package not installed. Run: pip install supermemory", file=sys.stderr) |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | +SCRIPT_DIR = Path(__file__).parent.resolve() |
| 36 | +DEFAULT_DATA_PATH = str(SCRIPT_DIR / ".." / "data" / "locomo10.json") |
| 37 | +DEFAULT_RECORD_PATH = str(SCRIPT_DIR / "result" / ".ingest_record.json") |
| 38 | + |
| 39 | + |
| 40 | +def sanitize_tag(raw: str) -> str: |
| 41 | + """Sanitize a tag string to match openclaw-supermemory convention. |
| 42 | + e.g. 'conv-26' -> 'conv_26' |
| 43 | + """ |
| 44 | + tag = re.sub(r"[^a-zA-Z0-9_]", "_", raw) |
| 45 | + tag = re.sub(r"_+", "_", tag) |
| 46 | + tag = tag.strip("_") |
| 47 | + return tag |
| 48 | + |
| 49 | + |
| 50 | +def wipe_container(client: Supermemory, container_tag: str) -> int: |
| 51 | + """ |
| 52 | + Delete all documents in a containerTag using documents.list + deleteBulk. |
| 53 | + Returns number of documents deleted. |
| 54 | + """ |
| 55 | + all_ids: list[str] = [] |
| 56 | + page = 1 |
| 57 | + |
| 58 | + while True: |
| 59 | + response = client.documents.list( |
| 60 | + container_tags=[container_tag], |
| 61 | + limit=100, |
| 62 | + page=page, |
| 63 | + ) |
| 64 | + |
| 65 | + memories = getattr(response, "memories", None) |
| 66 | + if memories is None and isinstance(response, dict): |
| 67 | + memories = response.get("memories", []) |
| 68 | + |
| 69 | + if not memories: |
| 70 | + break |
| 71 | + |
| 72 | + for doc in memories: |
| 73 | + doc_id = getattr(doc, "id", None) or (doc.get("id") if isinstance(doc, dict) else None) |
| 74 | + if doc_id: |
| 75 | + all_ids.append(doc_id) |
| 76 | + |
| 77 | + # Check pagination |
| 78 | + pagination = getattr(response, "pagination", None) or (response.get("pagination") if isinstance(response, dict) else None) |
| 79 | + total_pages = None |
| 80 | + if pagination: |
| 81 | + total_pages = getattr(pagination, "totalPages", None) or (pagination.get("totalPages") if isinstance(pagination, dict) else None) |
| 82 | + |
| 83 | + if total_pages is None or page >= total_pages: |
| 84 | + break |
| 85 | + page += 1 |
| 86 | + |
| 87 | + if not all_ids: |
| 88 | + return 0 |
| 89 | + |
| 90 | + # Delete in batches of 100 |
| 91 | + deleted = 0 |
| 92 | + for i in range(0, len(all_ids), 100): |
| 93 | + batch = all_ids[i : i + 100] |
| 94 | + client.documents.delete_bulk(ids=batch) |
| 95 | + deleted += len(batch) |
| 96 | + |
| 97 | + return deleted |
| 98 | + |
| 99 | + |
| 100 | +def clear_ingest_records(container_tag: str, record_path: str) -> int: |
| 101 | + """Remove ingest records for the given container_tag. Returns count removed.""" |
| 102 | + try: |
| 103 | + with open(record_path, "r", encoding="utf-8") as f: |
| 104 | + record = json.load(f) |
| 105 | + except (FileNotFoundError, json.JSONDecodeError): |
| 106 | + return 0 |
| 107 | + |
| 108 | + # Records are keyed as "supermemory:{sample_id}:{session_key}" |
| 109 | + # Match by sanitized sample_id to handle keys like "conv-26" vs "conv_26" |
| 110 | + keys_to_remove = [k for k in record if len(k.split(":")) >= 2 and sanitize_tag(k.split(":")[1]) == container_tag] |
| 111 | + |
| 112 | + for k in keys_to_remove: |
| 113 | + del record[k] |
| 114 | + |
| 115 | + with open(record_path, "w", encoding="utf-8") as f: |
| 116 | + json.dump(record, f, indent=2, ensure_ascii=False) |
| 117 | + |
| 118 | + return len(keys_to_remove) |
| 119 | + |
| 120 | + |
| 121 | +def delete_container(client: Supermemory, sample_id: str, record_path: str) -> bool: |
| 122 | + container_tag = sanitize_tag(sample_id) |
| 123 | + print(f" [containerTag={container_tag}] listing documents...", file=sys.stderr) |
| 124 | + |
| 125 | + try: |
| 126 | + deleted = wipe_container(client, container_tag) |
| 127 | + if deleted == 0: |
| 128 | + print(f" [WARN] No documents found (may already be deleted)", file=sys.stderr) |
| 129 | + else: |
| 130 | + print(f" [OK] Deleted {deleted} documents", file=sys.stderr) |
| 131 | + except Exception as e: |
| 132 | + print(f" [ERROR] Failed to delete documents: {e}", file=sys.stderr) |
| 133 | + return False |
| 134 | + |
| 135 | + removed = clear_ingest_records(container_tag, record_path) |
| 136 | + if removed: |
| 137 | + print(f" Cleared {removed} ingest record(s)", file=sys.stderr) |
| 138 | + |
| 139 | + return True |
| 140 | + |
| 141 | + |
| 142 | +def main() -> None: |
| 143 | + parser = argparse.ArgumentParser(description="Delete all Supermemory documents for given sample(s)") |
| 144 | + parser.add_argument("samples", nargs="*", help="sample_id(s) to delete (e.g. conv-26 conv-31)") |
| 145 | + parser.add_argument("--api-key", default=None, help="Supermemory API key (or SUPERMEMORY_API_KEY env var)") |
| 146 | + parser.add_argument("--from-data", action="store_true", help="load sample_ids from locomo10.json") |
| 147 | + parser.add_argument("--input", default=DEFAULT_DATA_PATH, help="path to locomo10.json") |
| 148 | + parser.add_argument("--limit", type=int, default=None, help="max samples to delete (with --from-data)") |
| 149 | + parser.add_argument( |
| 150 | + "--record", |
| 151 | + default=DEFAULT_RECORD_PATH, |
| 152 | + help=f"Path to ingest progress record (default: {DEFAULT_RECORD_PATH})", |
| 153 | + ) |
| 154 | + args = parser.parse_args() |
| 155 | + |
| 156 | + api_key = args.api_key or os.environ.get("SUPERMEMORY_API_KEY", "") |
| 157 | + if not api_key: |
| 158 | + print("Error: Supermemory API key required (--api-key or SUPERMEMORY_API_KEY env var)", file=sys.stderr) |
| 159 | + sys.exit(1) |
| 160 | + |
| 161 | + sample_ids: list[str] = list(args.samples) |
| 162 | + |
| 163 | + if args.from_data: |
| 164 | + with open(args.input, "r", encoding="utf-8") as f: |
| 165 | + data = json.load(f) |
| 166 | + if args.limit: |
| 167 | + data = data[: args.limit] |
| 168 | + sample_ids += [s["sample_id"] for s in data] |
| 169 | + |
| 170 | + if not sample_ids: |
| 171 | + print("Error: no sample_ids specified. Pass sample_ids or use --from-data", file=sys.stderr) |
| 172 | + sys.exit(1) |
| 173 | + |
| 174 | + sample_ids = list(dict.fromkeys(sample_ids)) # deduplicate, preserve order |
| 175 | + print(f"Deleting documents for {len(sample_ids)} sample(s)...", file=sys.stderr) |
| 176 | + |
| 177 | + client = Supermemory(api_key=api_key) |
| 178 | + ok = 0 |
| 179 | + for sid in sample_ids: |
| 180 | + print(f"\n=== {sid} ===", file=sys.stderr) |
| 181 | + if delete_container(client, sid, args.record): |
| 182 | + ok += 1 |
| 183 | + |
| 184 | + print(f"\nDone: {ok}/{len(sample_ids)} succeeded", file=sys.stderr) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + main() |
0 commit comments