Delete Attachments
-
This article explains how to delete attachments from documents in RavenDB.
-
Deleting a LOCAL attachment:
- The reference to the attachment is removed from the document’s metadata.
- The binary content is deleted from local storage only if no other documents in the database reference the exact same content.
-
Deleting a REMOTE attachment:
- Only the reference to the remote attachment is removed from the document’s metadata.
- RavenDB does not delete the binary data from the remote storage.
It is your responsibility to remove the file from the cloud provider manually.
-
When deleting a DOCUMENT:
- All local attachments referenced by the document are also deleted,
(as long as no other document references the same binary content). - Remote attachments are not deleted from the cloud provider - you must remove them manually.
- All local attachments referenced by the document are also deleted,
-
In this article:
Delete attachment using the Studio
In the Studio, open the document view.
To delete an attachment from the document, click the trash can icon next to the attachment name.

Delete attachment using the Client API
Delete attachment - via the session
- Delete_attachment
- Delete_attachment_async
using (var session = store.OpenSession())
{
Employee employee = session.Load<Employee>("employees/1");
// Mark the attachment for deletion:
// Provide the document entity and the attachment name
session.Advanced.Attachments.Delete(employee, "image1.png");
// Or - provide the document ID and the attachment name
// session.Advanced.Attachments.Delete("employees/1", "image1.png");
// The attachment is deleted when 'SaveChanges" is called
session.SaveChanges();
}
using (var asyncSession = store.OpenAsyncSession())
{
Employee employee = await asyncSession.LoadAsync<Employee>("employees/1");
// Mark the attachment for deletion:
// Provide the document entity and the attachment name
asyncSession.Advanced.Attachments.Delete(employee, "image1.png");
// Or - provide the document ID and the attachment name
// asyncSession.Advanced.Attachments.Delete("employees/1", "image1.png");
// The attachment is deleted when 'SaveChangesAsync' is called
await asyncSession.SaveChangesAsync();
}
Delete attachment - via an operation
- Delete_attachment
- Delete_attachment_async
// Define the delete attachment operation
// Specify the document ID and the attachment name
var deleteAttachmentOp = new DeleteAttachmentOperation("employees/1", "image1.png", null);
// Execute the operation by passing it to 'Operations.Send'
store.Operations.Send(deleteAttachmentOp);
// Define the delete attachment operation
// Specify the document ID and the attachment name
var deleteAttachmentOp = new DeleteAttachmentOperation("employees/1", "image1.png", null);
// Execute the operation asynchronously by passing it to 'Operations.SendAsync'
await store.Operations.SendAsync(deleteAttachmentOp);
syntax
Delete attachment via operation
public DeleteAttachmentOperation(string documentId, string name, string changeVector = null)
| Parameter | Type | Description |
|---|---|---|
| documentId | string | The ID of the document from which to delete the attachment. |
| name | string | The name of the attachment to delete. |
| changeVector | string | The document's change vector used for concurrency control. If set to null, no concurrency check will be performed and the attachment will be deleted regardless of the document's current state. |
Delete attachment via session
// Avialable overloads:
// ====================
void Delete(object entity, string name);
void Delete(string documentId, string name);
| Parameter | Type | Description |
|---|---|---|
| documentId | string | The ID of the document from which to delete the attachment. |
| entity | string | The document entity from which to delete the attachment. |
| name | string | The name of the attachment to delete. |