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

Delete attachment - via a patch

You can delete an attachment when patching documents using a script.
Learn about patching in: Patching single document and Patching multiple documents.

// Define the patch request 
var patchRequest = new PatchRequest
{
// Define a script that deletes the attachment
Script = "attachments(this, args.name).delete();",
Values =
{
{ "name", "image1.png" }
}
};

// Execute the patch operation
store.Operations.Send(new PatchOperation("employees/1", null, patchRequest));

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

// Available 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.
entityobjectThe document entity from which to delete the attachment.
namestringThe name of the attachment to delete.

Delete attachment via patch script

attachments(document, attachmentName).delete()
ParameterTypeDescription
documentobjectThe document entity (use this for the current document being patched).
attachmentNamestringThe name of the attachment to delete.

See JavaScript engine for the full list of JavaScript methods available in RavenDB.

In this article