How to delete node in a func

How do you delete a node in a func?

For starters I have the following:

curl localhost:8080/alter -XPOST -d $'
	{
		deleteusername(func: eq(user.name, "Alice")) {
      uid
      delete {
        uid # and maybe * * .
      }
		}
	}
' | python -m json.tool | less

I get:

{
    "errors": [
        {
            "code": "Error",
            "message": "Unexpected token: lex.Item [6] \"{\" while parsing schema"
        }
    ]
}

You’ve got a typo in the URL for starters: /alter is used for schema changes, /mutate is for mutations.

Yes, thanks Jesper

So now I am at this error:

{
“errors”: [
{
“code”: “ErrorInvalidRequest”,
“message”: “Invalid mutation formatting.”
}
]
}

In addition to what Jesper said. You do not perform deletions with Query.
You need to use RDF or JSON. Follow links.

RDF

https://docs.dgraph.io/mutations/#mutations-using-curl
https://docs.dgraph.io/mutations/#delete

JSON

https://docs.dgraph.io/mutations/#deleting-edges
https://docs.dgraph.io/mutations/#json-syntax-using-raw-http-or-ratel-ui

Thanks Michel

I did play around with this, do you know how I can delete without knowing the uid upfront?

Dgraph query and mutation are distinct operations that do not mix.
You must first query, collect the UIDs and then do the deletion process.

Sorry, but there must be is something I totally dont get.

From:
https://docs.dgraph.io/mutations/#mutations-using-curl

Running from terminal, this does not work:

curl -X POST localhost:8080/mutate -d $'
{
  set {
    _:alice <name> "Alice" .
  }
}'

I tried also using docker exec dgraph curl ...

It does work from Ratel UI.

But mostly in connection to the original question, I am unable to delete from terminal:

curl -X POST localhost:8080/mutate -d $'
{
  delete {
    _:alice <name> "Alice" .
  }
}'

Or Ratel:

{
  delete {
    _:alice <name> "Alice" .
  }
}

I get:
: uid not found/generated for xid _:alice

And if you are supposed to query first and then delete, using uid -I dont understand the documentation:

curl -X POST localhost:8080/mutate -d $'
{
  delete {
    _:alice <name> "Alice" .
  }
}'

Hope you can help, thanks in advance

Shouldn’t you also set the commit headers?
curl -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' …

Good thinking, but as I understand it - the json part is:

curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d  $'
    {
      "delete": [
        {
          "uid": "0xa"
        }
      ]
    }' | jq

Where as:

curl -X POST localhost:8080/mutate -d $'
{
  delete {
    _:alice <name> "Alice" .
  }
}'

is the way dgraph is doing there own json RDF version - but what do I know :slight_smile:

Thanks

First point. Mutation / Delation does not query. That is, it will not find “Alice” for you. The _:alice is a identifier known as “blank node”.

You need to first find Alice via query and then use like that

curl -X POST localhost:8080/mutate -d $'
{
  delete {
    <0x56f33> * * .   #the "0x56f33" would be Alice's UID
  }
}'

The JSON format for deletion you can find in Get started with Dgraph

Also, please do our Tour https://tour.dgraph.io/

I did go through all https://tour.dgraph.io/

Very good tour!

I just think the documentation is misleading by giving the samples, that is all - thanks.

curl -X POST localhost:8080/mutate -d $'
{
  delete {
    _:alice <name> "Alice" .
  }
}'

oh yeah! I see now. It’s my fault. I didn’t noticed it.
I’ll fix that, thanks!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.