Dpla takedown

From dpla/ingestion3 · View interactive version

Dpla takedown is an AI agent skill from the GitHub repository dpla/ingestion3. Remove DPLA items from the live site and search index in response to takedown requests. Use when the user says take down, delete, remove item(s) from dp.la, or action a takedown request. Accepts DPLA IDs directly or criteria (hub, institution, collection) to generate an ID list. Includes mandatory p

How to use this skill

Skill file

DPLA Takedown

Script

ingest_pipeline_python/helper_scripts/takedown.py runs this whole procedure for explicit IDs (one hub per run) and stops on any mismatch. Prefer it; use the phases below for criteria-based lists (Phase 0 B/C), Calisphere source checks, or a step that has to be redone by hand.

cd ingest_pipeline_python
../venv/bin/python helper_scripts/takedown.py -f ids.txt --dry-run   # lookup, source check, live-index count; deletes nothing
../venv/bin/python helper_scripts/takedown.py -f ids.txt             # asks before deleting

It stops on anything it can't confirm: an unresolved API lookup (only a 404 or an empty 200 means "already gone"), an unreadable count or alias, a live-index count that doesn't match, an S3 step that doesn't report its removal count, an alias swap or Elasticsearch error mid-delete. Each of those stops exits nonzero with its own message. If it gets through the deletes, the final check prints TAKEDOWN DONE only when every ID it took down is gone from the API and both count deltas check out; otherwise TAKEDOWN INCOMPLETE OR UNVERIFIED and a nonzero exit. Treat any nonzero exit as needing investigation. TAKEDOWN DONE covers only the IDs in the manifest's take-down list. Two kinds of requested IDs aren't on it, and neither is removed from S3: IDs the API didn't return (printed before the manifest under "Not in the API … skipped", first 10 only), and IDs left out because their source isn't confirmed removed (listed in the manifest under "Left out"). Before reporting the request done, compare the original request with the take-down list. Left-out IDs are either unverified (the script couldn't check the source) or live (the source still has them). For an unverified ID, check the source by hand; put only the ones you've confirmed gone in a new ID file and rerun with --include-unverified, which takes down every unverified ID in its input. --include-unverified never takes down a live ID: if your own check says a live item is gone, resolve that conflict (with the hub if needed) before removing it. For every API-missing ID, check the hub's newest S3 JSONL from the repo root with ./venv/bin/python scripts/delete/delete-from-jsonl.py --dry-run --bucket dpla-master-dataset --hub <s3-hub> --profile dpla -f <ids-file>. A dry run reads every file, changes nothing, and lists Matches per ID: an ID with a nonzero count is still in S3 and would come back at the next index rebuild. Only trust it if it exits 0 with no file errors. If any file failed, a 0 doesn't show the ID is gone, so rerun before reporting those IDs as checked. If S3 removes a different number than expected, it asks whether to continue to the live index; saying yes doesn't stop TAKEDOWN DONE, because the final check doesn't look at the S3 count. Resolve any S3 count mismatch before reporting the request done. It doesn't touch unsynced snapshots on EC2.

Purpose

Safely remove one or more DPLA items from the live dp.la search index and from the

S3 master dataset (so they do not reappear on the next full index rebuild). This is

the most sensitive operation in the ingestion system — an error here affects the

production aggregation directly.

When to Use

Threat Model

| Risk | Mitigation |

|------|-----------|

| Malformed ES query deletes wrong records | Mandatory search-before-delete: run the exact query as _search first, confirm hit count matches expected count, show results to user |

| SSM command escaping corrupts query JSON | Write all SSM payloads to JSON files and pass via --cli-input-json file://... — no shell escaping |

| S3 JSONL file corrupted during filter/upload | S3 versioning is enabled on dpla-master-dataset — previous versions are recoverable |

| Network failure during S3 re-upload | Re-run the S3 delete step; the script is idempotent (re-filtering an already-filtered file is a no-op) |

| Wrong hub identified for S3 delete | Derive hub from DPLA API metadata, not user input; cross-check with user before proceeding |

| Criteria-based query returns too many IDs | Always show the full count AND a sample of records to the user; require explicit confirmation before proceeding |

| Record reappears after next harvest | Warn user that upstream source must also remove the record; no persistent exclusion list exists |

Prerequisites

Pre-Run Snapshot (MANDATORY for count delta verification)

Before any other step, capture a baseline. This is used in Phase 4 to verify only the expected records were removed.

export DPLA_API_KEY=$(grep '^DPLA_API_KEY=' ~/.claude/secrets/dpla.env | cut -d= -f2)
TOTAL_BEFORE=$(curl -s "https://api.dp.la/v2/items?api_key=$DPLA_API_KEY&page_size=1" | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])")
echo "Total DPLA items before: $TOTAL_BEFORE"
aws sts get-caller-identity --profile dpla 2>/dev/null && PROFILE="dpla" || PROFILE="default"
echo "Using AWS profile: $PROFILE"

Also capture the hub count once the hub is known (after Phase 0/1 identifies it):

HUB_BEFORE=$(curl -s "https://api.dp.la/v2/items?api_key=$DPLA_API_KEY&page_size=1&provider.name=<hub-name>" | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])")
echo "Hub items before: $HUB_BEFORE"

Python Preflight (run once before Phase 2)

Run this single block to discover the correct Python binary with boto3 installed:

PYTHON=""
for p in "./venv/bin/python" "/opt/homebrew/bin/python3.9" "/opt/homebrew/bin/python3" "python3"; do
  if command -v "$p" &>/dev/null && "$p" -c "import boto3" 2>/dev/null; then
    PYTHON="$p"
    break
  fi
done
if [ -z "$PYTHON" ]; then
  # boto3 not installed — attempt install then retry
  pip3 install boto3 -q && PYTHON="python3"
fi
echo "PYTHON=$PYTHON"
"$PYTHON" --version

Record the value of $PYTHON — use it verbatim in all subsequent Python invocations.

Workflow

Phase 0: Determine the ID list

The user provides ONE OF:

A) Explicit IDs — one or more 32-character DPLA IDs (hex strings). Proceed to Phase 1.

B) A dp.la search URL — e.g. https://dp.la/search?partner=...&provider=...&collection=.... In this case:

  1. Parse the URL query parameters to extract filter values. The relevant mappings are (from the dp.la frontend source):
  1. URL-decode all parameter values before using them in API queries.
  2. Build the API query and proceed as in option C below (paginate, show sample, confirm).

C) Criteria — e.g. "all items from [institution] in [hub]", or "collection X". In this case:

  1. Query the DPLA API to generate the list. Use provider.name and/or admin.contributingInstitution and/or sourceResource.collection.title filters with page_size=500 and paginate to collect all matching IDs:

```bash

export DPLA_API_KEY=$(grep '^DPLA_API_KEY=' ~/.claude/secrets/dpla.env | cut -d= -f2)

curl -s "https://api.dp.la/v2/items?provider.name=<hub+display+name>&admin.contributingInstitution=<institution>&page_size=500&page=1&api_key=$DPLA_API_KEY"

```

> IMPORTANT — sourceResource.collection.title uses tokenized (full-text) search, not exact matching. A query for "Maps - Marin County" will also return records from "Maps - Marin County Plat Maps, J.C. Oglesby Collection" because the tokens appear in that longer title. Always post-filter results with a client-side exact string match on collection.title after paginating:

> ```python

> exact = [r for r in records

> if any(c.get('title') == TARGET_COLLECTION

> for c in (r['sourceResource'].get('collection') or [])

> if isinstance(c, dict))]

> ```

> The API count and the dp.la website count may differ by the number of partial-match false positives; the post-filtered count is authoritative.

  1. STOP and show the user:
  1. Only proceed after explicit user confirmation.

Phase 1: Pre-flight verification (MANDATORY — never skip)

For every ID in the list:

  1. Look up via DPLA API:

```bash

. ~/.claude/secrets/dpla.env

curl -s "https://api.dp.la/v2/items/<id>?api_key=$DPLA_API_KEY"

```

  1. Extract and record:
  1. Verify the item exists. If the API returns no results, warn the user and skip that ID.
  2. Verify the source record is gone (MANDATORY). For each item, determine the correct URL to check and verify it returns 404. This confirms the item has been deleted at the source institution and will not be re-harvested.

ContentDM (common case): ContentDM uses a React SPA that always returns HTTP 200 for the page shell regardless of whether the item or collection exists — do NOT rely on HTTP status codes from /cdm/ref/collection/ or /digital/collection/ URLs. Use the ContentDM REST API instead:

```bash

# Extract collection alias and item ID from isShownAt URL

# isShownAt format: http://<host>/cdm/ref/collection/<alias>/id/<n>

# or: http://<host>/digital/collection/<alias>/id/<n>

HOST="<contentdm-hostname>"

ALIAS="<collection-alias>" # e.g. "stanleymisc"

ITEM_ID="<n>" # e.g. "0"

# 1. Check if the entire collection is unpublished (most efficient for batch takedowns)

curl -o /dev/null -s -w "%{http_code}" "http://$HOST/digital/api/collections/$ALIAS"

# 404 → entire collection removed → all items confirmed deleted

# 2. Check individual item existence

curl -o /dev/null -s -w "%{http_code}" "http://$HOST/digital/api/collections/$ALIAS/items/$ITEM_ID"

# 404 → item removed ✓

# NOTE: Do NOT use the legacy dmwebservices endpoint:

# /digital/bl/dmwebservices/index.php?q=dmGetItemInfo/<alias>/<id>/json

# This endpoint returns item metadata even for unpublished/restricted collections

# and is NOT a reliable indicator that the item is publicly accessible.

```

Calisphere / CDL ARK items: Calisphere (calisphere.org) is behind AWS WAF which returns 202 + x-amzn-waf-action: challenge to all non-browser curl requests, making HTTP status checks impossible via curl. Use the browser automation tools instead:

```javascript

// Run in-browser via mcpClaude_in_Chromejavascript_tool (avoids WAF)

// First navigate to calisphere.org to establish session, then:

const urls = ["https://calisphere.org/item/ark:/13030/<ark1>", ...];

window._results = null;

Promise.all(urls.map(url =>

fetch(url, {method: 'GET', redirect: 'follow'})

.then(r => ({url, status: r.status}))

.catch(e => ({url, status: 'ERROR'}))

)).then(r => { window._results = r; });

// Then check: JSON.stringify(window._results)

```

  1. Determine S3 hub prefix. The API slug (e.g. orbiscascade) may differ from the S3 prefix (e.g. orbis-cascade). Check the actual S3 prefix:

```bash

aws s3 ls s3://dpla-master-dataset/ --profile $PROFILE | grep -i <hub-slug>

```

  1. Group IDs by hub. All IDs in a single S3 delete run must belong to the same hub.
  2. Capture the hub baseline count (see Pre-Run Snapshot above) using the hub's provider.name.
  3. Present the full manifest to the user:

```

Takedown Manifest

=================

Items: N

Hub(s): <hub list>

ID Title DataProvider Source Status

-------------------------------- ----------------------------------- -------------------------- ---------------

57d000d66004c73c5e31ea7dc7f57201 "Letter from John Adams, 1776" Massachusetts Historical Society 404 ✓

...

Excluded (source still live — not taking down):

<id> "<title>" <dataProvider> 200 ✗

...

This will:

1. Remove these items from the S3 master dataset (s3://dpla-master-dataset/<hub>/jsonl/)

2. Remove these items from the live Elasticsearch index

3. Items will no longer appear on dp.la

WARNING: There is no persistent exclusion list. If the hub re-harvests

and these items are still in their source feed, they will reappear after

the next ingest + index rebuild. Coordinate with the hub to remove from

their source.

Proceed? [y/N]

```

  1. Wait for explicit user confirmation. Do not proceed without it.

Phase 2: Delete from S3 JSONL

This prevents the items from reappearing on the next full ES index rebuild.

For each hub in the manifest:

  1. Determine AWS profile:

```bash

aws sts get-caller-identity --profile dpla 2>/dev/null && PROFILE="dpla" || PROFILE="default"

echo "Using profile: $PROFILE"

```

  1. Write IDs to a temp file (one per line):

```bash

IDFILE=$(mktemp /tmp/dpla-takedown-XXXXXX.txt)

printf '%s\n' <id1> <id2> ... > "$IDFILE"

cat "$IDFILE"

```

  1. Run the delete-from-jsonl script using the Python discovered in the preflight:

```bash

# Run from repository root

$PYTHON scripts/delete/delete-from-jsonl.py \

--hub <s3-hub-prefix> \

--profile $PROFILE \

-y \

-f "$IDFILE"

```

The script handles .gz, .jsonl, and .json (uncompressed) file extensions.

  1. Check the output. Verify:
  1. If the script fails: Do NOT proceed to ES delete. Report the error to the user. S3 versioning allows recovery if any files were partially modified.

Phase 3: Delete from Elasticsearch

This removes items from the live search index immediately.

Step 3a: Resolve the index name from dpla_alias

Write the SSM payload to a local file to avoid shell escaping issues:

cat > /tmp/ssm-resolve-alias.json << 'ENDJSON'
{
  "InstanceIds": ["i-00bbdfe0a6ff6cf78"],
  "DocumentName": "AWS-RunShellScript",
  "Parameters": {
    "commands": ["IP=$(hostname -I | cut -d' ' -f1); curl -s http://$IP:9200/_alias/dpla_alias | python3 -c \"import sys,json; print(list(json.load(sys.stdin).keys())[0])\""]
  }
}
ENDJSON

RESOLVE_CMD_ID=$(aws ssm send-command \
  --cli-input-json file:///tmp/ssm-resolve-alias.json \
  --query 'Command.CommandId' --output text --profile $PROFILE)

echo "SSM command: $RESOLVE_CMD_ID"
sleep 8

INDEX_NAME=$(aws ssm get-command-invocation \
  --command-id "$RESOLVE_CMD_ID" \
  --instance-id i-00bbdfe0a6ff6cf78 \
  --query 'StandardOutputContent' --output text \
  --profile $PROFILE | tr -d '[:space:]')

echo "Resolved index: $INDEX_NAME"

Validate the index name: It must match the pattern dpla-all-YYYYMMDD-HHMMSS. If it does not, STOP and report the error.

Step 3b: Search-before-delete (MANDATORY — never skip)

Build the query JSON locally (no escaping issues), then pass to SSM via --cli-input-json:

# Build the query file
python3 -c "
import json
ids = [<quoted-id-list>]
print(json.dumps({'query': {'terms': {'id': ids}}}))
" > /tmp/dpla-takedown-query.json

python3 -m json.tool /tmp/dpla-takedown-query.json > /dev/null && echo "JSON valid"

# Transfer query via base64 to remote instance, then search using file reference.
# This avoids all JSON escaping issues in the SSM shell command.
python3 - "$INDEX_NAME" << 'PYEOF' > /tmp/ssm-search.json
import json, base64, sys
idx = sys.argv[1]
with open('/tmp/dpla-takedown-query.json') as f:
    query = f.read().strip()
b64 = base64.b64encode(query.encode()).decode()
cmd = (f"echo {b64} | base64 -d > /tmp/dpla-q.json && "
       f"IP=$(hostname -I | cut -d' ' -f1) && "
       f"curl -s -XPOST \"http://$IP:9200/{idx}/_search?size=10\" "
       f"-H 'Content-Type: application/json' -d @/tmp/dpla-q.json")
payload = {
    'InstanceIds': ['i-00bbdfe0a6ff6cf78'],
    'DocumentName': 'AWS-RunShellScript',
    'Parameters': {'commands': [cmd]}
}
print(json.dumps(payload))
PYEOF

SEARCH_CMD_ID=$(aws ssm send-command \
  --cli-input-json file:///tmp/ssm-search.json \
  --query 'Command.CommandId' --output text --profile $PROFILE)

sleep 8

SEARCH_RESULT=$(aws ssm get-command-invocation \
  --command-id "$SEARCH_CMD_ID" \
  --instance-id i-00bbdfe0a6ff6cf78 \
  --query 'StandardOutputContent' --output text \
  --profile $PROFILE)

echo "$SEARCH_RESULT"
HIT_COUNT=$(echo "$SEARCH_RESULT" | python3 -c "import sys,json; print(json.load(sys.stdin)['hits']['total']['value'])")
echo "Expected: <N>  Actual: $HIT_COUNT"

Verify the search result:

```

ES Pre-Delete Verification

==========================

Index: dpla-all-20260202-164030

Query: terms match on N IDs

Expected: N hits

Actual: N hits ✓

Proceed with deletion? [y/N]

```

Step 3c: Execute the delete

python3 - "$INDEX_NAME" << 'PYEOF' > /tmp/ssm-delete.json
import json, base64, sys
idx = sys.argv[1]
with open('/tmp/dpla-takedown-query.json') as f:
    query = f.read().strip()
b64 = base64.b64encode(query.encode()).decode()
cmd = (f"echo {b64} | base64 -d > /tmp/dpla-q.json && "
       f"IP=$(hostname -I | cut -d' ' -f1) && "
       f"curl -s -XPOST \"http://$IP:9200/{idx}/_delete_by_query?conflicts=abort\" "
       f"-H 'Content-Type: application/json' -d @/tmp/dpla-q.json")
payload = {
    'InstanceIds': ['i-00bbdfe0a6ff6cf78'],
    'DocumentName': 'AWS-RunShellScript',
    'Parameters': {'commands': [cmd]}
}
print(json.dumps(payload))
PYEOF

DELETE_CMD_ID=$(aws ssm send-command \
  --cli-input-json file:///tmp/ssm-delete.json \
  --query 'Command.CommandId' --output text --profile $PROFILE)

sleep 12

DELETE_RESULT=$(aws ssm get-command-invocation \
  --command-id "$DELETE_CMD_ID" \
  --instance-id i-00bbdfe0a6ff6cf78 \
  --query 'StandardOutputContent' --output text \
  --profile $PROFILE)

echo "$DELETE_RESULT"

Verify the delete result:

Phase 4: Post-deletion verification (MANDATORY — never skip)

Step 4a: Verify each ID is gone from the DPLA API

For each deleted ID, confirm the API returns 0 results:

export DPLA_API_KEY=$(grep '^DPLA_API_KEY=' ~/.claude/secrets/dpla.env | cut -d= -f2)
for ID in <id1> <id2>; do
  RESP=$(curl -s "https://api.dp.la/v2/items/${ID}?api_key=$DPLA_API_KEY")
  if echo "$RESP" | grep -q "could not be found"; then
    echo "ID $ID: not found ✓"
  else
    COUNT=$(echo "$RESP" | python3 -c "import sys,json; print(json.load(sys.stdin).get('count','?'))" 2>/dev/null || echo "?")
    echo "ID $ID: count=$COUNT ✗ STILL PRESENT"
  fi
done

Note: There may be a brief API cache delay (up to a few minutes) before the deletion is reflected. If an ID still shows as present, wait 2 minutes and retry before declaring a failure.

Step 4b: Verify via dp.la web (recommended for single-item takedowns)

Navigate to https://dp.la/item/<id> and confirm it returns a 404 or "item not found" page.

Step 4c: Count delta verification

Confirm that only the expected number of items were removed — and no others:

. ~/.claude/secrets/dpla.env
TOTAL_AFTER=$(curl -s "https://api.dp.la/v2/items?api_key=$DPLA_API_KEY&page_size=1" | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])")
HUB_AFTER=$(curl -s "https://api.dp.la/v2/items?api_key=$DPLA_API_KEY&page_size=1&provider.name=<hub-name>" | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])")

echo "Total before: $TOTAL_BEFORE  |  Total after: $TOTAL_AFTER  |  Delta: $((TOTAL_BEFORE - TOTAL_AFTER))"
echo "Hub before:   $HUB_BEFORE    |  Hub after:   $HUB_AFTER    |  Delta: $((HUB_BEFORE - HUB_AFTER))"
echo "Expected delta: <N>"

Expected outcomes:

Step 4d: Report final status to the user:

Takedown Complete
=================
Items deleted: N
S3 JSONL: N records removed from s3://dpla-master-dataset/<hub>/jsonl/<export>/
Elasticsearch: N records deleted from index <index-name>
API verification: N items confirmed removed (count: 0)
dp.la page: 404 confirmed ✓

Count delta:
  Total DPLA: <before> → <after>  (Δ = -N) ✓
  Hub <name>: <before> → <after>  (Δ = -N) ✓

REMINDER: These items will reappear if the hub re-harvests and they are
still in the upstream source. Coordinate with the hub to remove from source.

Phase 5: Cleanup

rm -f /tmp/dpla-takedown-*.json /tmp/dpla-takedown-*.txt /tmp/ssm-*.json

Critical Rules

Recovery Procedures

| Failure | Recovery |

|---------|----------|

| S3 JSONL file corrupted | Restore previous version via S3 versioning: aws s3api list-object-versions --bucket dpla-master-dataset --prefix <key> then aws s3api get-object --bucket dpla-master-dataset --key <key> --version-id <version-id> <local-file> |

| Wrong records deleted from ES | Full re-index from S3 JSONL via sparkindexer on EMR (multi-hour operation). The S3 JSONL is the authoritative dataset. |

| S3 delete succeeded but ES delete failed | Re-run only the ES delete (Phase 3). The S3 state is already correct. |

| ES delete succeeded but S3 delete failed | Re-run only the S3 delete (Phase 2). Items are gone from live site but will reappear on next index rebuild if S3 is not fixed. |

| Script fails mid-file during S3 processing | Re-run the S3 delete script — it will re-process all files. Already-removed IDs simply won't be found again (idempotent). |

| Count delta > expected | Investigate immediately: check ES _search for unexpectedly missing IDs; if needed, trigger full re-index from S3 |

Limitations

Key References

| Resource | Path |

|----------|------|

| Delete from ES script | scripts/delete/delete-by-id.sh |

| Delete from S3 JSONL (Python) | scripts/delete/delete-from-jsonl.py |

| S3 master dataset | s3://dpla-master-dataset/<hub>/jsonl/ |

| ES nodes (SSM-managed) | i-00bbdfe0a6ff6cf78 (search-prod1), all nodes in SSM |

| DPLA API | https://api.dp.la/v2/items/<id>?api_key=$DPLA_API_KEY |

| ES alias | dpla_alias → dpla-all-YYYYMMDD-HHMMSS |

| Agent guide | AGENTS.md |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.