sparqlgraphdbrdf4jnamed-graphs

Most efficient way to clear a named graph?


I am working with an instance of Ontotext GraphDB and often want to clear a named graph with a large number of triples.

Currently, my technique involves issuing a SPARQL command to the graph server, which searches and matches a triple pattern of every triple in the named graph:

DELETE { GRAPH example:exampleGraph { ?s ?p ?o }} WHERE {?s ?p ?o .}

When there are a lot of triples, this approach often takes quite some time to clear the named graph.

I am wondering whether there is a more efficient way to do this. Even a triplestore-specific solution would be acceptable to me.

I should also note that I am using the RDF4J library to communicate with the graph. I understand that certain solutions may work on the Ontotext web interface, but I am only interested in a solution which I can implement programatically.


Solution

  • You can use the SPARQL CLEAR command for this:

    CLEAR GRAPH example:exampleGraph
    

    Or alternatively, DROP:

    DROP GRAPH example:exampleGraph
    

    The difference between the two is that CLEAR allows triplestores to keep an empty named graph, while the DROP completely removes the named graph. But in the case of GraphDB there's no practical difference as GraphDB never keeps a reference to an empty named graph.

    If you don't want to use SPARQL, you can use the RDF4J API to programmatically call the clear() operation:

    IRI graph = graphdb.getValueFactory().createIRI("http://example.org/exampleGraph");  
    try(RepositoryConnection conn = graphdb.getConnection()) {
       conn.clear(graph);
    }
    

    or more succinctly:

    IRI graph = graphdb.getValueFactory().createIRI("http://example.org/exampleGraph");  
    Repositories.consume(graphdb, conn -> conn.clear(graph));