Skip to main content

Overview

The unlink() method removes a directional relation between two resources. This is useful for cleaning up outdated connections or correcting mistakes.
Unlinking only removes the specified direction. If you created bidirectional links, you must unlink both directions separately.

Method Signature

def unlink(from_uri: str, uri: str) -> None

Parameters

from_uri
string
required
Source URI of the relation to remove
uri
string
required
Target URI of the relation to remove

Examples

Remove Single Relation

from openviking import OpenViking

client = OpenViking(path="./data")

# Remove a relation
client.unlink(
    from_uri="viking://resources/code/auth.py",
    uri="viking://resources/docs/old-auth.md"
)

Clean Up Outdated Relations

# Get all relations
relations = client.relations("viking://resources/code/main.py")

# Remove outdated ones
for rel in relations:
    if "deprecated" in rel['to_uri']:
        client.unlink(
            from_uri=rel['from_uri'],
            uri=rel['to_uri']
        )
        print(f"Removed: {rel['to_uri']}")

Remove All Relations for a Resource

# Before deleting a resource, clean up its relations
uri = "viking://resources/temp/example.md"

# Remove all outgoing relations
relations = client.relations(uri)
for rel in relations:
    client.unlink(from_uri=uri, uri=rel['to_uri'])

# Now safe to delete
client.rm(uri)

Response

{
  "status": "ok",
  "time": 0.002
}

Error Responses

Relation Not Found

If the relation doesn’t exist, the operation succeeds silently (idempotent):
{
  "status": "ok",
  "time": 0.001
}

Use Cases

  1. Cleanup - Remove outdated or incorrect relations
  2. Refactoring - Update relations when resources move or change
  3. Maintenance - Clean up relations before deleting resources
  4. Corrections - Fix accidentally created relations

Best Practices

Always query relations with relations() before mass unlinking to avoid removing unexpected links.
# Good: Verify before removing
relations = client.relations(uri)
print(f"Found {len(relations)} relations")

# Review and selectively unlink
for rel in relations:
    if should_remove(rel):
        client.unlink(uri, rel['to_uri'])

# Avoid: Blind removal without checking
# client.unlink(uri, some_uri)  # What if this removes something important?

Notes

  • Unlink is idempotent: removing a non-existent relation succeeds
  • Unlink only affects one direction: A → B unlink doesn’t affect B → A
  • Unlink does not delete resources: only removes the relation
  • Resources can still be found via semantic search after unlinking
  • link() - Create relations between resources
  • relations() - Query existing relations
  • rm() - Delete resources (does not auto-unlink)