Skip to content

Instantly share code, notes, and snippets.

@tomsaleeba
Created May 15, 2018 08:01
Show Gist options
  • Save tomsaleeba/e82bbe327c6ef9a22e60e6f92cbcf3a4 to your computer and use it in GitHub Desktop.
Save tomsaleeba/e82bbe327c6ef9a22e60e6f92cbcf3a4 to your computer and use it in GitHub Desktop.
Deleting batches of records with SPARQL

I learned this when trying to clear our records in AWS Neptune. I was hitting the query timeout when trying to drop an entire graph. If you don't want to/can't raise the timeout, you can drop smaller parts of the graph in each transaction.

curl -sX POST http://<cluster-prefix>.rds.amazonaws.com:8182/sparql --data-urlencode 'update=
DELETE {
  GRAPH <http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph> { ?s ?p ?o }
}
WHERE {
  GRAPH <http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph> {
    {
      SELECT ?s ?p ?o
      WHERE {
        ?s ?p ?o .
      }
      LIMIT 10
    }
  }
}
'

This will delete 10 records, specifically the first 10 that are returned for a SELECT * WHERE { ?s ?p ?o } query. You can adjust the limit value to find a batch size that keeps you under the timeout.

Yeah, this is a dirty hack but there was a bit of pain to learn this so I want to store the knowledge.

Also, be sure to use --data-urlencode not --data-binary otherwise you might find the server ignores your input but doesn't give any indication of error.

@jimsmart
Copy link

jimsmart commented Mar 12, 2023

To delete all of the contents of a named graph, instead of iterating over the triples, it is much easier and quicker to use:

DROP GRAPH <http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph>

Or perhaps:

CLEAR GRAPH <http://aws.amazon.com/neptune/vocab/v01/DefaultNamedGraph>

Add the SILENT keyword before GRAPH if the graph might not exist, but you don't want that to return an error.

— Links to the docs, for more info:
https://www.w3.org/TR/sparql11-update/#drop
https://www.w3.org/TR/sparql11-update/#clear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment