Delete Data with Named Tags and SQL
NCache provides Object Queries to delete the particular item from the cache having the Named Tag and its value as provided. Ensure that the item is added to the cache with the Named Tags. Refer to the Add Items with Named Tags section to get a detail on creating and adding Named Tags.
- To learn about the standard prerequisites required to work with all NCache client-side features please refer to the given page on Client Side API Prerequisites.
- Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
- For API details, refer to: ICache, ICacheReader, QueryCommand, Remove, ExecuteNonQuery, SearchService.
An example below shows how to delete items associated with a Named Tag using queries.
// Preconditions: Cache is already connected
// Items are already present in the cache with Named Tags
// A user wants to end the VIP membership discount offer So this Named Tag needs to be removed from the customer.
// All the customers with VIP membership discount Named Tag needs to be removed from the cache.
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Make sure to use the Fully Qualified Name (FQN)
// Create an SQL Query with the specified criteria
string query = "DELETE FROM Alachisoft.NCache.Samples.Data.Customer WHERE VIP_Membership_Discount = 0.12 ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// 'rowsAffected' indicates the number of items removed from the cache
int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);
Console.WriteLine($"No VIP members removed");
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
To get more detail on object queries please refer to the SQL Reference for NCache section.
Another approach to removing data with Named Tags is to fetch the data by querying it and then using the Remove method on the fetched data to remove the selected values.
Using this approach, the items associated with a specific Named Tag are removed. However, it is not recommended as it will increase the required network calls which is not very efficient for the user.
For more details on the remove method, please refer to the Remove Named Tags From Cache Data docs.
NCache provides a sample application for Tags on GitHub.