remove()

Remove entities or relationships from your knowledge graph.

Signature

brain.remove(id: string): Promise<void>

Parameters

Property Type Required Description
id string Yes The entity or relationship ID to remove

Returns

A Promise that resolves when the removal is complete.

Examples

Remove an Entity

remove-entity.js
// Add an entity
const id = await brain.add({
  data: "Temporary data"
})

// Remove it
await brain.remove(id)

// Entity no longer exists
const entity = await brain.get(id)
console.log(entity) // null

Remove a Relationship

remove-relationship.js
// Create a relationship
const relationId = await brain.relate({
  from: entityA,
  to: entityB,
  type: "references"
})

// Remove the relationship (entities remain)
await brain.remove(relationId)

// Entities still exist
const a = await brain.get(entityA) // still exists
const b = await brain.get(entityB) // still exists

Cascade Delete

cascade-delete.js
// When removing an entity, its relationships are also removed

// Create entities and relationships
const parent = await brain.add({ data: "Parent" })
const child = await brain.add({ data: "Child" })
await brain.relate({ from: parent, to: child, type: "parentOf" })

// Remove parent - relationship is automatically removed
await brain.remove(parent)

// Child still exists, but relationship is gone
const childEntity = await brain.get(child) // still exists

Bulk Remove

bulk-remove.js
// Find entities to remove
const oldDrafts = await brain.find({
  where: {
    status: "draft",
    createdAt: { lessThan: "2023-01-01" }
  }
})

// Remove all matching entities
await Promise.all(
  oldDrafts.map(entity => brain.remove(entity.id))
)

console.log(`Removed ${oldDrafts.length} old drafts`)

Safe Remove

safe-remove.js
// Check if entity exists before removing
async function safeRemove(id) {
  const entity = await brain.get(id)
  if (entity) {
    await brain.remove(id)
    return true
  }
  return false
}

const removed = await safeRemove("maybe-exists-id")
console.log(removed ? "Removed" : "Not found")

Notes

With Vault (Enterprise)

If you're using Soulcraft Vault, removed entities can be recovered using time-travel queries:

vault-recovery.js
// With Vault, query data as it was before deletion
const beforeDelete = await brain.find({
  query: "deleted content",
  asOf: "2024-01-15T10:00:00Z"  // Time before deletion
})

See Also

Back to Quick Start Learn Triple Intelligence