How can we delete all documents from a collection using the Payload API?

default discord avatar
Above The Cloudslast year
3

What is the best way to delete all documents in a collection using the Payload API?



Using Mongoose we can simply use deleteMany() - but the Payload API is "forcing" the definition of a 'where' clause - which can't be empty otherwise no document is deleted:



await payload.delete({ collection: "users", where: { } });



Thanks in advance!



How can we delete all documents from a collection using the Payload API?

  • default discord avatar
    jarrod69420last year

    Could you say: where: { id: { exists: true } }

  • default discord avatar
    Above The Cloudslast year

    As simple as that! I am used to use '$exists' and didn't think of 'exist' - thanks lot!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.