Skip to main content

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.
  • 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

Delete attachment using the Client API

Delete attachment - via the session

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();
}

Delete attachment - via an operation

// 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);

syntax

Delete attachment via operation

public DeleteAttachmentOperation(string documentId, string name, string changeVector = null)    
ParameterTypeDescription
documentIdstringThe ID of the document from which to delete the attachment.
namestringThe name of the attachment to delete.
changeVectorstringThe 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);
ParameterTypeDescription
documentIdstringThe ID of the document from which to delete the attachment.
entitystringThe document entity from which to delete the attachment.
namestringThe name of the attachment to delete.
In this article