Reputation: 52725
What's the easiest way to purge all attachments from a RavenDB database?
I'm using the .NET API, but I'm open to doing a raw HTTP call.
Upvotes: 3
Views: 184
Reputation: 241525
This will go through all attachments in the database and delete them one at a time.
private void DeleteAllAttachments(IDocumentStore store)
{
while (true)
{
var header = store.DatabaseCommands
.GetAttachmentHeadersStartingWith("", 0, 1)
.FirstOrDefault();
if (header == null) return;
store.DatabaseCommands.DeleteAttachment(header.Key, null);
}
}
You could probably optimize it a bit by taking a larger page size, but since you are deleting anyway, it likely won't matter that much.
Upvotes: 3
Reputation: 6841
To my knowledge there is not a built in option to get all attachments for a database. But you can do the following:
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:8080");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
HttpResponseMessage response = client.GetAsync("databases/YourDatabaseName/static").Result;
response.EnsureSuccessStatusCode();
var files = response.Content.ReadAsAsync<IList<AttachmentInformation>>().Result;
foreach (var fileDetail in files)
{
store.DatabaseCommands.DeleteAttachment(fileDetail.Key, null);
}
For this to work you need the following POCO. And to a reference to Microsoft.AspNet.WebApi.Client v5. You can remove this requirement if you just do ReadAsString and parse the response content yourself or with another json parser.
public class AttachmentInformation
{
/// <summary>
/// Gets or sets the size.
/// </summary>
/// <value>The size.</value>
public int Size { get; set; }
/// <summary>
/// Gets or sets the key.
/// </summary>
/// <value>The key.</value>
public string Key { get; set; }
/// <summary>
/// Gets or sets the metadata.
/// </summary>
/// <value>The metadata.</value>
public object Metadata { get; set; }
/// <summary>
/// Gets or sets the etag.
/// </summary>
/// <value>The etag.</value>
public Guid Etag { get; set; }
}
Upvotes: 1