Discussion Forum

How to remove attributes that doesnt have any edge to an entity

while inserting data into database i have made some mistakes, there are some attributes that are not binded with an entity. is it possible to remove those attributes? some query to get all attributes that doesnt have any edge, either to an entity or relation

The question is basically how to identify and delete all disconnected attributes.

There’s a couple points around this:

  1. To identify all attributes without any owners only:
    match $a isa attribute; not { $_ has $a; };
  2. To identify attributes without any role players:
    match $a isa attribute; not { ($a) isa relation; };
  3. To identify attributes without owners nor role players:
    match $a isa attribute; not { $_ has $a; }; not { ($a) isa relation; };

The second two variations will throw an exception if it’s never possible in your schema that an attribute plays a role - use option 1. if attributes are only owned directly.

A note on memory: doing too many delete operations in a single transaction using the following query:

match $x isa attribute; not { $_ has $a; }; delete $a isa attribute;

may cause a memory overflow. Probably would happen when deleting tens or hundreds of thousands of attributes in a single transaction.

To mitigate this you need to batch the deletion from the application logic:

def delete_all_matching(session, query, var_to_delete, write_batch=100):
  read_tx = session.transaction(READ)
  matching = read_tx.query().match(query)
  for ans in matching:
      write_tx = session.transaction(WRITE)
      for i in range(write_batch):
        write_tx.query("match $x iid {0}; delete $x isa thing;".format(ans.get(var_to_delete))

this will delete batches of the indicated variable for all matching answers of the query, in batches of write_batch size. This guarantees not too many operations are performed in one write transaction at a time.

1 Like