rdfsparqltriplestore

SPARQL why are there both DELETE and WHERE conditions?


Recently I was deleting some data according to a matching pattern ?s a :answers and I notices that I could only get the deletion to correctly proceed if I had both condition blocks as the same:

PREFIX :      <http://localhost:2020/vocab/> 
DELETE {
    ?s a :answers
}
WHERE {
    ?s a :answers
}
# returns all the correct data

and if the delete block is more generalized, nothing is returned:

PREFIX :      <http://localhost:2020/vocab/>
DELETE {
    ?s ?p ?o
}
WHERE {
    ?s a :answers
}
# returns 0 rows

What is the purpose of having two different sets of conditions (the stuff between curly braces: {...} )? and which one actually selects the data to be deleted?


Solution

  • Because what you delete may not be what you match with WHERE.

    There is a special form for the first case:

    DELETE WHERE { ?s a :answers }
    

    In the second, ?p and ?o are not bound. Try:

    DELETE {
        ?s ?p ?o
    }
    WHERE {
        ?s a :answers .
        ?s ?p ?o .
    }