Registering deletions
Deleting a single file
In order to register a file delete operation you need to use the RegisterFileDeletion
method.
Syntax
There are two overloads:
void RegisterFileDeletion(string path, Etag etag = null);
void RegisterFileDeletion(FileHeader file, Etag etag = null);
Parameters | ||
---|---|---|
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) |
FileNotFoundException
If the requested file does not exist in the file system, the FileNotFoundException
will be thrown by the SaveChangesAsync
.
Example
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.
RegisterDeletionQuery
The first one is RegisterDeletionQuery
, which has the following signature:
Syntax
void RegisterDeletionQuery(string query);
Parameters | ||
---|---|---|
query | string | The Lucene query |
You need to specify the valid Lucene query as a string value.
Example
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();
RegisterResultsForDeletion
Deleting multiple files by querying is even more convenient when you can take advantage of the session's querying support.
Example
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();