List pages that have no links to them? (orphan pages)

I’m trying to reproduce this query in SilverBullet v2, but a query inside another doesn’t seem to work. Is there a way to do this?

The query I’m trying to make (simplified):

${query[[
  from p = index.tag "page"
  where not some(query[[
    from l = index.tag "link"
    where l.toPage == p.name
  ]])
]]}

Result is Failed to execute 'continue' on 'IDBCursor': The transaction has finished.

Right, indeed nested queries are not going to work. This is what I was able to come up with:

```space-lua
function orphanPages()
  -- Collect all page names of non meta-pages and put them in a map
  local orphans = {}
  for name in query[[
    from tags.page
    where not table.find(_.tags,
      function(t) return t:startsWith("meta") end)
    select _.name
  ]] do
    orphans[name] = true
  end
  
  -- Then delete all the ones that have links to them
  for linked in query[[from tags.link select _.toPage]] do
    orphans[linked] = nil
  end
  return table.keys(orphans)
end
```

${orphanPages()}

Caveat here is that I found a subtle bug that I fixed in 2.1.8 which I just released, so please upgrade to that before trying this.

1 Like

Thanks so much! It seems to work, at the bottom I get some weird stuff but I’ll try and figure it out.