How to Delete Cache Data for a Single Tag using SQL
NCache provides the ability to delete tagged data using SQL-like syntax via the ExecuteNonQuery method, allowing for efficient bulk removal in distributed environments. Object queries can also be used to remove items from the cache by using Tags as SQL query criteria. By using the $Tag$ keyword, NCache ensures that applications can identify and purge specific datasets without the overhead of manual iteration.
Prerequisites
- 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, ExecuteNonQuery, SearchService.
SQL DELETE for One Tag
Using object queries, items can be deleted using a single Tag. The following example deletes all the items associated with the Tag West Coast Customers using the SQL query.
// Precondition: Cache is already connected
// Make sure to use the Fully Qualified Name (FQN)
string query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";
// Create query command and add parameter
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "West Coast Customers");
// Executing Query
int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);
if (rowsAffected == 0)
Console.WriteLine($"No Customers from West Coast found");
Warning
Providing a Null Tag value for the query will throw an ArgumentNullException or NullPointerException.
Note
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 details on object queries, please refer to the SQL Reference for NCache section.
Additional Resources
NCache provides a sample application for Tags on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.runtime.caching class.
Node.js: Tag class.