Query to list blocks that have id's but are not referenced anywhere

Whenever we press CTRL + C, logseq adds an ID to the respective block. But if I accidentally hit CTRL + C and move on from that block, the id remains in that block.
This is not a big problem, but I would like to not have the ID if the block is not referenced.

So I’m looking for a query to list all block which have an id but are not reference anywhere.

Is this possible?


Note that is the opposite of this question:

Something like this:

#+BEGIN_QUERY
{:query [:find (pull ?b[*])
   :where
     [?b :block/properties ?props]
     [(get ?props :id) ?id]
     (not
       [?other :block/content ?content]
       [(!= ?other ?b)]
       [(clojure.string/includes? ?content ?id)]
     )
 ]
}
#+END_QUERY
1 Like

Yes, Thank you.

Although it looks like this takes time - probably since it needs to query all the blocks in the entire graph

Could try replacing the whole not-clause with (not [?other :block/refs ?b])

2 Likes

Awesome, This is very smooth with replaced the not clause

#+BEGIN_QUERY
{:query [:find (pull ?b[*])
   :where
     [?b :block/properties ?props]
     [(get ?props :id) ?id]
     (not [?other :block/refs ?b])
 ]
}
#+END_QUERY
1 Like