How to Configure LlamaIndex MongoDB Prevent Delete
You set up a llamaindex mongodb prevent delete rule by giving the app a read-only database role. You can also build a custom role that leaves out the remove and dropCollection actions. Many teams start by giving their AI app a broad read-and-write user account. That quick choice lets a bad prompt or agent bug wipe out whole collections. How llamaindex mongodb prevent delete works with database roles LlamaIndex connects to your database using the user account you put in the connection string.
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 a llamaindex mongodb prevent delete rule by giving the app a read-only database role. You can also build a custom role that leaves out the remove and dropCollection actions. Many teams start by giving their AI app a broad read-and-write user account. That quick choice lets a bad prompt or agent bug wipe out whole collections.
How llamaindex mongodb prevent delete works with database roles
LlamaIndex connects to your database using the user account you put in the connection string. The framework sends queries or writes vectors based on the tools you give the agent. LlamaIndex docs warn that running open queries from a model carries real security risks. The docs suggest using restricted roles, read-only databases, and sandboxes to stay safe.
MongoDB controls access through role-based rules that apply to a database or a single collection. Each role holds a list of allowed actions on a specific resource. Both self-hosted servers and cloud clusters build their user rules from these same basic actions.
The simplest way to block deletes is to assign the built-in read role. The read role lets the app run find, listCollections, listIndexes, dbStats, collStats, and changeStream actions. It leaves out the insert, update, remove, and dropCollection actions on non-system collections. When your app only needs to read documents for answers, the read role stops all deletes.
How to allow inserts while blocking deletes in MongoDB
Some AI apps need to save new chat logs or write vector data into a collection. The built-in read role will block those writes because it lacks the insert action. Teams often switch to the built-in readWrite role to fix this issue.
The readWrite role adds insert and update actions, so the agent can save new data. Still, readWrite also adds remove, dropIndex, and dropCollection actions to that same user account. If you use readWrite, the agent can once again delete documents or drop a whole collection.
To allow writes without deletes, you must create a custom role in the database. You grant that role the find, insert, and update actions on the target collection. You leave the remove and dropCollection actions out of the custom role definition. You then assign this custom role to the database user that your AI app uses. Now the database will reject any delete command that comes from the agent.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native roles solve and what they leave open
Database roles do a great job of blocking forbidden actions at the storage engine level. If an agent tries to run a remove action with a read role, the database refuses. This setup decides which user can log in and which broad action types can run.
Still, static database roles cannot check the context of an individual request in flight. A role that allows updates to save vector data can still overwrite existing records with blank fields. That bad update acts just like a delete, even when the remove action is off.
Static roles also do not rewrite sensitive fields when the agent reads a collection. If a document holds emails, phone numbers, card numbers, or national IDs, the read role returns them. Native roles also attach to a shared app user rather than the person asking the question. Teams still need a way to inspect commands in flight and mask sensitive fields in results.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev runs beside your database as a sidecar for your MongoDB. It checks every wire command and result document without changing your drivers or credentials.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run. Data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
Does the MongoDB read role block all delete commands from LlamaIndex?
Yes, the built-in read role blocks all data changes on non-system collections in that database. It allows read actions like find and listCollections, and it leaves out remove and dropCollection.
Why is the readWrite role risky for an AI agent?
The readWrite role grants insert and update actions, and it also includes remove and dropCollection. Any agent using a readWrite account can delete records or drop a collection if prompted.
How can an AI app insert vectors without having delete rights?
You can make a custom role in MongoDB that grants only find, insert, and update actions. Leaving remove and dropCollection off the custom role lets the app write data while blocking deletes.
Related guides
In this post