Search code examples
mongodbgrailsgrails-orm

Grails/MongoDB delete all


I have a Grails app with Mongodb in backend. I am trying to delete all the records of a collection as soon as a function is executed. This is what I tried in my ResourceController:

def report()
{
    Resource.where { }.deleteAll() //expected to delete all records from resource collection.
}

It doesn't work.

def report()
{
    def p = Resource.findAll()
    p.delete()
}

Neither approached worked. Is there any way to delete all records from a collection as soon as a method is executed? I read the documentation and it says that GORM doesn't support delete all. If anyone knows any workaround please let me know.


Solution

  • You should be able to remove all with the following syntax:

    Resource.collection.remove(new BasicDBObject());
    

    assuming Resource is a collection.