# Find orphaned attachments (objects)

**URL:** <https://community.silverbullet.md/t/find-orphaned-attachments-objects/4373>\
**Category:** Tricks & Techniques\
**Created:** [September 15, 2026, 8:45pm UTC](https://community.silverbullet.md/t/find-orphaned-attachments-objects/4373 "2026-09-15T20:45:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coffeemug13](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Coffeemug13](https://community.silverbullet.md/u/Coffeemug13)\
**Post date:** [September 15, 2026, 8:45pm UTC](https://community.silverbullet.md/t/find-orphaned-attachments-objects/4373/1 "2026-09-15T20:45:13Z")

</div>

Hi, can you give me a hint, how to finde orphaned attachments in a Silverbullet Space? I didn't find a clue in the documentation.  
Other Tools provides features or Plugins to identify attachments, which are no longer linked from pages.

---

<div class="post-metadata">

**Author:** ![Coffeemug13](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/c/919ad9/32.png) [@Coffeemug13](https://community.silverbullet.md/u/Coffeemug13)\
**Post date:** [September 18, 2026, 7:50pm UTC](https://community.silverbullet.md/t/find-orphaned-attachments-objects/4373/2 "2026-09-18T19:50:42Z")

</div>

Here is my solution. Open tree view and select "view document". When you click on the orphan document you will see in the tree the document selected (feature of SB). There is an icon to delete the document.

# Function to detect orphan documents

```space-lua
function orphanDocuments()
  local documents = {}

  -- search for all documents
  local docResult = query[[from tags.document select _.name]] or {}
  for name in docResult do
    if name then
      documents[name] = true
    end
  end

  -- search for all links to documents
  local relResult = query[[
    from tags.relation
    where _.toTag == "document"
    select _.to
  ]] or {}

-- remove documents if linked
  for target in relResult do
    if target then
      documents[target] = nil
    end
  end

  local orphans = table.keys(documents)
  table.sort(orphans)

  if #orphans == 0 then
    return "No orphan documents found"
  end

  -- Build a markdown list with wiki links
  local lines = {}
  for _, name in ipairs(orphans) do
    table.insert(lines, "* [[" .. name .. "]] - " .. name)
  end
  return table.concat(lines, "\n")
end

```

# List orphan documents

${orphanDocuments()}
