# Deleting RDF List items with SPARQL/Update
Inspired by Bob DuCharme's post "[RDF Lists and SPARQL](http://www.snee.com/bobdc.blog/2014/04/rdf-lists-and-sparql.html)", this post presents a SPARQL Update query capable of looking for a given item in a list and removing it, even if it is in the first position.
For instance, given the following list in Turtle notation:
```turtle
:fruits :hasList ("apple" "orange" "banana") .
```
Which is actually, without the lists' syntactic sugar, implemented by the following RDF triples serialised using Turtle too:
```turtle
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:fruits :hasList
[rdf:first "apple"; rdf:rest
[rdf:first "orange"; rdf:rest
[rdf:first "banana"; rdf:rest rdf:nil ]]] .
```
The SPARQL/Update query to remove a given item from the list (in this particular case "orange") is:
```sparql
DELETE {
?deletionPoint ?prop ?listNode .
?listNode rdf:first ?target ; rdf:rest ?rest . }
INSERT {
?deletionPoint ?prop ?rest. }
WHERE {
BIND( "orange" AS ?target)
?list rdf:rest*/rdf:first ?target .
?deletionPoint ?prop ?listNode .
?listNode rdf:first ?target ; rdf:rest ?rest .
}
```
This query, in its `WHERE` part, first locates inside a list the target element to be removed, which is associated to the `?target` variable using the `BIND` clause.
The query then sets the deletion point, which is the resource pointing to the list node that contains the target item (i.e. the one pointed by the `rdf:first` property).
The property linking the deletion point and the list node to be removed is usually the `rdf:rest` property of the previous list node. However, for the first node, it is the property linking to the first node (in this particular case :hasList). Consequently, the `?prop` variable has been introduced to accomodate both cases.
Once the list node to be removed has been located, together with the deletion point, the property linking to it and the rest of the list following the node, it is time for deletions and insertions.
First, the triple pointing from the deletion point to the list node to be removed is deleted. The `rdf:first` and `rdf:rest` triples for the list node are also removed.
Finally, a new triple linking the deletion point, using the appropriate property, to the rest of the list following the removed list node is inserted.