public bool Remove(int id) { try { var filtro_foto = Builders <Photo> .Filter.Eq("Id", id); DeleteResult actionResult = _context.Photos.DeleteOne(filtro_foto); List <Specie> especies = _context.Species.Find(_ => true).ToList(); foreach (Specie especie in especies) { int indice = especie.Gallery.FindIndex(f => f.Id == id); if (indice != -1) { especie.Gallery.RemoveAt(indice); _SpecieRepository.UpdateGallery(especie.Id, especie.Gallery); DirectoryInfo DI = new DirectoryInfo(Constants.RUTA_ARCHIVOS_IMAGENES_ESPECIES + especie.Id.ToString() + "/"); foreach (var file in DI.GetFiles()) { string[] extension = (file.Name).Split('.'); if (extension[0] == id.ToString()) { string fileAddress = DI.FullName + file.Name; File.Delete(fileAddress); break; } } break; } } return(actionResult.IsAcknowledged && actionResult.DeletedCount > 0); } catch (Exception ex) { Console.Write("error: " + ex.StackTrace + "\n"); Console.Write("error: " + ex.Message + "\n"); throw ex; } }