Search code examples
pythonsparqlamazon-neptune

Delete/clear all contents of a neptune named graph using sparql


I am trying to delete/clear data from a named graph using python. Used approach

import requests
url = "neptune_endpoint:8182/sparql"
query = "CLEAR GRAPH IRIref."
PARAMS = {"query" :query}
r = requests.get(url, params= PARAMS)

getting error malformed query. Thanxs in advance for help.


Solution

  • The following code should work for you.

    import requests
    url = "neptune_url:8182/sparql"
    query = "CLEAR GRAPH IRIref"
    headers = {"content-type": "application/sparql-update"}
    r = requests.post(url, data=query, headers=headers)
    

    Note that the specification requires this to be a POST and have specific content type headers set. Neptune is fully SPARQL 1.1 compliant, so the relevant areas in the specification here are https://www.w3.org/TR/sparql11-protocol/#update-operation and https://www.w3.org/TR/sparql11-update/#clear.