How to Configure Dify MongoDB Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up dify mongodb prevent delete rules by assigning custom database roles to your AI user. You must withhold remove and drop actions while keeping read or insert rights active. Many teams start by giving their AI workflow the standard read and write role. That broad role lets a bad prompt erase documents or drop whole collections. How to set up dify mongodb prevent delete with native roles MongoDB controls what users can do through role-based authorization on each database. A role gives

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up dify mongodb prevent delete rules by assigning custom database roles to your AI user. You must withhold remove and drop actions while keeping read or insert rights active. Many teams start by giving their AI workflow the standard read and write role. That broad role lets a bad prompt erase documents or drop whole collections.

How to set up dify mongodb prevent delete with native roles

MongoDB controls what users can do through role-based authorization on each database. A role gives a user permission to run specific actions on defined resources. You can scope these permissions down to an individual collection in your database. Note that cloud database users and self-hosted users have different built-in role options. Even so, both setups build roles from the same basic privilege actions.

If your Dify workflow only answers questions, assign the built-in read role. The read role lets the app find records and list collections or indexes. It also allows viewing database stats, collection stats, and change streams. The read role leaves out insert, update, remove, and collection drop actions. Your AI agent can read data for prompts without changing any stored records.

Sometimes an AI workflow needs to save new chat logs or user notes. The standard read and write role includes insert and update actions for this job. Still, that built-in role also includes remove, drop index, and drop collection actions. To stop deletions, you must build a custom role in your database settings. Grant the read actions along with insert and update on the target collection. Leave the remove and drop collection actions out of this custom role. Then create a dedicated database user for Dify and attach this custom role.

What native roles solve and what they leave open

Native database roles stop unauthorized delete commands at the storage engine level. If Dify sends a remove action, the database checks the role and rejects it. This setup protects your collections from total loss when an agent hallucinates.

Still, static database roles only check who logs in at the start. They do not inspect what individual queries do in flight during a session. A user with update rights can still overwrite a document with blank fields. That bad update destroys the old data just like a delete command would. Also, native roles apply the same static rules to every query from Dify. You cannot pause a risky write to ask a human for approval first.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing shared keys and sensitive data in AI workflows

Storing a static database key inside Dify creates another operational risk for teams. Anyone who can view the app settings can copy that database password. If a team shares one key across many workflows, audit logs lose clarity. You can see that the Dify user ran a query in the log. You cannot tell which workflow or person triggered that database action.

Read queries from an AI agent also pull raw records into prompt context. Those records often hold emails, phone numbers, card numbers, or national IDs. Database roles decide if a user can read a collection or not. They do not rewrite matched fields in results before the app reads them.

Inspect MongoDB wire traffic in flight

Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev is an open-source proxy that sits next to MongoDB as a sidecar for your MongoDB. It reads every command from mongosh, drivers, or agent tools.

First, hoop.dev guardrails block bad deletes, drops, and write pipelines. Next, hoop.dev data masking rewrites sensitive values in returned documents based on your rules and logs each decision. Explore the repo on GitHub.

Frequently asked questions

Can the built-in readWrite role block delete commands in MongoDB?

No, the built-in readWrite role always includes the remove and dropCollection actions. You must create a custom role without those actions to block deletes.

Does the MongoDB read role stop Dify from dropping collections?

Yes, the built-in read role only allows finding data and viewing stats. It does not permit insert, update, remove, or dropCollection actions on the database.

Can I restrict Dify delete permissions to a single MongoDB collection?

Yes, MongoDB roles can grant permissions down to the collection level. You can allow writes on one collection while keeping others read-only.

The query runs. The emails stay hidden.