Raven DB: How can I delete all documents of a given type

后端 未结 4 848
旧巷少年郎
旧巷少年郎 2021-02-04 04:52

More specifically in Raven DB, I want to create a generic method with a signature like;

public void Clear() {...

Then have Raven DB cl

4条回答
  •  迷失自我
    2021-02-04 05:33

    I had this problem as well and this is the solution that worked for me. I'm only working in a test project, so this might be slow for a bigger db, but Ryan's answer didn't work for me.

        public static void ClearDocuments(this IDocumentSession session)
        {
            var objects = session.Query().ToList();
            while (objects.Any())
            {
                foreach (var obj in objects)
                {
                    session.Delete(obj);
                }
    
                session.SaveChanges();
                objects = session.Query().ToList();
            }
        }
    

提交回复
热议问题