Litedb: Delete all items from collection

Created on 14 Feb 2020  路  3Comments  路  Source: mbdavid/LiteDB

With LiteDB v4 is was used to write Delete(Query.All()). This doesn't work with v5 anymore. Also using DeleteMany(_ => true) gets me an exception.

How can I delete all entries from a collection?

bug fixed question

Most helpful comment

Hi @JensSchadron, I just add support for unary parameter as predicate in LINQ converter.

All 3 comments

@GW-FUB Not completely sure how it's supposed to be, but this does the job 馃檪

class Program
{
    static void Main(string[] args)
    {
        using var db = new LiteDatabase(new MemoryStream());
        var col = db.GetCollection<TestClass>();
        col.Insert(new TestClass());
        col.Insert(new TestClass());

        Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)\n");

        Console.WriteLine($"Deleting item(s) in collection {nameof(TestClass)}\n");
        col.DeleteMany("1=1");

        Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)\n");
    }

    class TestClass
    {
        public ObjectId Id { get; set; }
        public string Data { get; set; }
    }
}

Hi @JensSchadron, I just add support for unary parameter as predicate in LINQ converter.

@mbdavid I tested it and it seems to work indeed. Therefor I'll close this issue.

@GW-FUB Not sure if you're still interested in this, but you can now also pass _ => true to the DeleteMany() method.

Was this page helpful?
0 / 5 - 0 ratings