You are currently browsing legacy 3.5 version of documentation. Click here to switch to the newest 5.1 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Registering deletions

Deleting a single file

In order to register a file delete operation you need to use the RegisterFileDeletion method.


There are two overloads:

void RegisterFileDeletion(string path, Etag etag = null);
void RegisterFileDeletion(FileHeader file, Etag etag = null);
path string The full file path
file FileHeader The file represented by FileHeader
etag Etag Current file Etag, used for concurrency checks (null will skip the check)


If the requested file does not exist in the file system, the FileNotFoundException will be thrown by the SaveChangesAsync.


session.RegisterFileDeletion(await session.LoadFileAsync("/txt/1.txt"));

await session.SaveChangesAsync();

Deleting multiple files based on search results

You can also register deletion of multiple files that match certain criteria. Then the actual delete operation is performed based on results of the specified query. There are two methods accessible from the session that allows you to pass the query that will be used to determine which files have to be deleted.


The first one is RegisterDeletionQuery, which has the following signature:


void RegisterDeletionQuery(string query);
query string The Lucene query

You need to specify the valid Lucene query as a string value.


The below code deletes all files which names end with .draft (note the usage of the built-in __rfileName term, click here to see more built-in index fields)


await session.SaveChangesAsync();


Deleting multiple files by querying is even more convenient when you can take advantage of the session's querying support.


The below code registers exactly the same files for deletion, in this case however the query is specified in strongly typed manner:

	.WhereEndsWith(x => x.Name, ".draft")

await session.SaveChangesAsync();