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

default discord avatar
Above The Clouds
4 months ago
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
    jarrod69420
    4 months ago

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

  • default discord avatar
    Above The Clouds
    4 months ago

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

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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