private static void PruneResults(Index index, IDictionary<ItemUri, bool> crawledItems, bool fullCrawl) { IPruner pruner; if (fullCrawl) { pruner = new DictionaryPruner(crawledItems); } else { pruner = new SitecorePruner(); } IndexSearchContext searchContext = index.CreateSearchContext(); IndexReader reader = searchContext.Searcher.Reader; List<int> itemsToDelete = new List<int>(); try { // first get how many documents are in the index int numberOfDocuments = reader.NumDocs(); // interate over all of the documents for (int i = 0; i < numberOfDocuments; i++) { // check to see if the document is deleted if (reader.IsDeleted(i)) { continue; } // get the actual document from the index Document doc = reader.Document(i); // get the url field from the index which contains item id, version, database, etc. Field field = doc.GetField(BuiltinFields.Url); string path = field.StringValue(); // parse out the values from the index ItemUri uri = new ItemUri(path); if (!pruner.StillExists(uri)) { itemsToDelete.Add(i); } } } finally { // clean up after ourselves reader.Close(); searchContext.Searcher.Close(); if (itemsToDelete.Any()) { using (IndexDeleteContext context = index.CreateDeleteContext()) { context.DeleteDocuments(itemsToDelete); context.Commit(); } } } }