Delete with an index

Is it possible to delete a specific document using the Index?
I have tried it with this code:
client := f.NewFaunaClient(key)
_, err = client.Query(f.Delete(f.MatchTerm(f.Index(“url_short”), url.Short)))
Which sadly got me this error: Errors: [delete](invalid argument): Ref expected, Set provided."

The Delete function wants you to be specific about what is to be deleted, which is why it expects a reference and not a set of items.

Match (or for Go, MatchTerm) provides a set. That might be zero, one, or more items.

The standard way to make a set “concrete” is to use Paginate, and then loop over the results. That means your query could look like this:

_, err = client.Query(
    f.Paginate(f.MatchTerm(f.Index("url_short"), url.Short)),

The combination of Map and Paginate handles all of the set sizes, e.g. nothing happens if the set is empty. However, the default page size for Paginate is 64. If you need to delete more documents in one go, you’d need to set the page size accordingly. If you have more documents to delete than the maximum page size (100K documents), then you have to execute multiple queries.

If you are confident that your set matches a single document (say, because you have an index that makes the search term unique), then you can use Singleton instead:

_, err = client.Query(
  f.Delete(f.Singleton(f.MatchTerm(f.Index("url_short"), url.Short))))
1 Like

Thank you for your quick reply.
I have tried both of your solutions and that fixed my issue.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.