Try Playground
Show / Hide Table of Contents

Delete Tag Data from Cache with SQL Query

Object queries can also be used to remove items from the cache with your SQL query criteria being tags. Object queries let you search the data in your cache and then delete it. The idea of a special keyword $Tag$ also exists here. The ExecuteNonQuery is used in queries containing delete operations. It returns the number of affected rows after the query is executed.

You can remove the items present with a specific tag as explained below.

Prerequisites to Delete Tag Data from Cache Using SQL Query

  • .NET
  • Java
  • Python
  • Node.js
  • 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.
  • 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: Cache, CacheReader, executeNonQuery, QueryCommand, getSearchService.
  • 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: Cache, CacheReader, execute_non_query, QueryCommand getSearchService.
  • 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: Cache, executeReader, QueryCommand, CacheReader, get_search_service.

SQL DELETE for One Tag

Using object queries, items can be deleted using a single tag. You can delete a single tag using the SQL query as explained in the example below.

The following example deletes all the items associated with the tag West Coast Customers using the SQL query.

  • .NET
  • Java
  • Python
  • Node.js
// Precondition: Cache is already connected
// Make sure to use the Fully Qualified Name (FQN)
string query = "DELETE FROM Alachisoft.NCache.Samples.Data.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");
// Pre-condition: Cache is already connected
// Create SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
String query = "DELETE FROM com.alachisoft.ncache.sample.Customer WHERE $Tag$ = ?";

// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("$Tag$", "West Coast Customers");

// Execute the query
int rowsAffected = cache.getSearchService().executeNonQuery(queryCommand);

if (rowsAffected == 0) {
    System.out.println("No Customers from West Coast found");
}
# Precondition: Cache is already connected
# Items are already present in the cache with tags
# Custom class is query indexed through NCache Management Center or config.ncconf

# Make sure to use the Fully Qualified Name(FQN)
query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?"

# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
query_command.set_parameters({"$Tag$": "Important Customers"})

search_service = cache.get_search_service()

# rows_affected is the number of items removed from cache

rows_affected = search_service.execute_non_query(query_command)
// Precondition: Cache is already connected
// This is an async method
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Management Center or config.ncconf

// Make sure to use the Fully Qualified Name (FQN)
var query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";

// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
queryCommand.getParameters().set("$Tag$", "Important Customers");

var searchService = await this.cache.getSearchService();

// rowsAffected is the number of items removed from cache
var rowsAffected = await searchService.executeReader(queryCommand);
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 detail 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.

In This Article
  • Prerequisites to Delete Tag Data from Cache Using SQL Query
  • SQL DELETE for One Tag
  • Additional Resources
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top