Unable to delete

I’ve gone thru every google search and example I can find, but I must be doing something wrong. I am unable to actually delete linked nodes.

Dgraph version : v1.2.2
Dgraph SHA-256 : 86e27921d3a53dbc4faba78e9c7d7879ce8bafd6006c9fd4b77220535b8ae1c2
Commit SHA-1 : 21cc8936
Commit timestamp : 2020-03-19 12:13:11 -0700
Branch : HEAD
Go version : go1.13.5

Schema example


type Person {
  person_name
  person_hasPet
}

person_name: string @index(term, fulltext) .
person_hasPet:[uid] @count @reverse .

type Pet {
  pet_name
  pet_isAnimal
}

pet_name: string @index(term, fulltext) .
pet_isAnimal:[uid] @count @reverse .

type Animal {
  animal_name
}
animal_name:string @index(term,fulltext) .

Sample Data

{
	"set": [{
			"uid": "_:animal3",
			"dgraph.type": "Animal",
			"animal_name": "reptile"
		},
		{
			"uid": "_:animal4",
			"dgraph.type": "Animal",
			"animal_name": "avian"
		},
		{
			"dgraph.type": "Person",
			"person_name": "Pablo",
			"person_hasPet": [{
					"dgraph.type": "Pet",
					"pet_name": "Spot",
					"pet_isAnimal": {
						"uid": "_:animal3"
					}
				},
				{
					"dgraph.type": "Pet",
					"pet_name": "Polly",
					"pet_isAnimal": {
						"uid": "_:animal4"
					}
				}
			]
		}
	]
}

Verify the data set looks good

{
  q(func: type(Person)) {
    uid
    expand(_all_) {
      uid
      expand(_all_) {
	uid
	expand(_all_)
      }
    }
  }
}

Query returns something like this

{
  "data": {
    "q": [
      {
        "uid": "0x4e4c",
        "person_name": "Pablo",
        "person_hasPet": [
          {
            "uid": "0x4e4d",
            "pet_name": "Spot",
            "pet_isAnimal": [
              {
                "uid": "0x4e4f",
                "animal_name": "reptile"
              }
            ]
          },
          {
            "uid": "0x4e4e",
            "pet_name": "Polly",
            "pet_isAnimal": [
              {
                "uid": "0x4e4b",
                "animal_name": "avian"
              }
            ]
          }
        ]
      }
    ]
  }
}

Use case: Pablo had two pets, but one died, so I want to remove the pet “Spot” from Pablo.
I want to keep his second pet as is. And I don’t want to delete the Animal node “reptile” from dgraph as other Animals will have the same uid link.

Nothing seems to work correctly I’m always left with 0x4e4d hanging there which makes my query ‘count(person_hasPet)’ incorrect.

Various mutations I’ve tried (which all return “Success” not matter what).

{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d",
	"pet_isAnimal": {
	   "uid": "0x4e4f"
	}
   }
}
{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d"
   }
}


{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d",
	"pet_isAnimal": null
   }
}
{
	"delete": [{
			"uid": "0x4e4e",
			"dgraph.type": "Pet",
			"pet_name": "Polly",
			"pet_isAnimal": {
				"dgraph.type": "Animal",
				"uid": "0x4e4b"
			}
		},
		{
			"dgraph.type": "Animal",
			"uid": "0x4e4b"
		}
	]
}

Can anyone point out my mistake(s)?

First thing, never add the Type or any other attribute that you don’t have to delete. Adding the “dgraph.type” into the delete operation, it will delete the type.

In delete operations you insert only the UID and what you need to delete or patterns of deletion.

This operation will delete dgraph.type and the relation between 0x4e4d and 0x4e4f via pet_isAnimal.

{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d",
	"pet_isAnimal": {
	   "uid": "0x4e4f"
	}
   }
}

This operation will delete only the dgraph.type

{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d"
   }
}

This operation will delete dgraph.type and all relations via pet_isAnimal.

{
  "delete": {
	"dgraph.type": "Pet",
	"uid": "0x4e4d",
	"pet_isAnimal": null
   }
}

And the last one is a mix of all the others.

This operation below will first delete the relation, and then the pet itself.

{
   "delete": [
      {
         "uid": "0x4e4c",
         "person_hasPet": {
            "uid": "0x4e4d"
         }
      },
      {
         "uid": "0x4e4d"
      }
   ]
}

Ok, this would be a kind of “ontology”. Nice.

Well, that’s it.

Cheers.

1 Like

There is this video that can be helpful

@MichelDiz thank you so much! I’ve been going crazy trying to figure out the correct syntax to do this.