# MongoServerError: cannot delete from a capped collection

- **ID:** `mongodb/capped-collection-deletion`
- **Domain:** mongodb
- **Category:** runtime_error
- **Error Code:** `20`
- **Verification:** ai_generated
- **Fix Rate:** 92%

## Root Cause

Attempted to delete a document from a capped collection, which only supports insertion and reading, not deletion or update without replacement.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb-3.6 | active | — | — |
| mongodb-4.0 | active | — | — |
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## Workarounds

1. **Convert the capped collection to an uncapped collection using `convertToCapped` or create a new uncapped collection and copy data via aggregation with $out. Example: `db.runCommand({ convertToCapped: 'myCollection', size: 100000 });` then drop and recreate as uncapped.** (85% success)
   ```
   Convert the capped collection to an uncapped collection using `convertToCapped` or create a new uncapped collection and copy data via aggregation with $out. Example: `db.runCommand({ convertToCapped: 'myCollection', size: 100000 });` then drop and recreate as uncapped.
   ```
2. **Use `db.collection.drop()` to remove the entire capped collection (data loss) and recreate it as a regular collection with `db.createCollection('myCollection')`.** (95% success)
   ```
   Use `db.collection.drop()` to remove the entire capped collection (data loss) and recreate it as a regular collection with `db.createCollection('myCollection')`.
   ```
3. **Instead of deleting, set a TTL index or use a capped collection with a max document count to automatically age out old data. Example: `db.createCollection('logs', { capped: true, size: 5242880, max: 5000 });`** (90% success)
   ```
   Instead of deleting, set a TTL index or use a capped collection with a max document count to automatically age out old data. Example: `db.createCollection('logs', { capped: true, size: 5242880, max: 5000 });`
   ```

## Dead Ends

- **** — allowDiskUse applies to aggregation pipelines, not to deletion operations on capped collections. (15% fail)
- **** — Dropping the capped collection removes all data permanently; a workaround should preserve data if needed. (10% fail)
- **** — The error is triggered by the operation type, not by the filter; any delete attempt on a capped collection fails. (5% fail)
