Deleting a single file
In order to register a file delete operation you need to use the
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
|etag||Etag||Current file Etag, used for concurrency checks (
If the requested file does not exist in the file system, the
FileNotFoundException will be thrown by the
session.RegisterFileDeletion("/movies/intro.avi"); 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)
session.RegisterDeletionQuery("__rfileName:tfard.*"); 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:
session.Query() .WhereEndsWith(x => x.Name, ".draft") .RegisterResultsForDeletion(); await session.SaveChangesAsync();