• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Delete Tag Data from Cache with SQL

Note

This feature is only available in NCache Enterprise Edition.

Object queries can also be used to remove items from cache with your 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. ExecuteNonQuery is used in queries containing delete operations. It returns the number of affected rows after query is executed.

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

Prerequisites

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
  • 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.
  • Make sure that the data be 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, 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, executeReader, QueryCommand, CacheReader, get_search_service.
  • 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.

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/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
try
{
    // Pre-condition: 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");
}
catch (OperationFailedException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures 
        // Operation Timeout
        // Operation performed during state transfer    
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException    
}
try
{
    // Pre-condition: Cache is already connected
    // Items are already present in the cache with tags
    // Custom class is query indexed through NCache Web Manager or config.ncconf

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

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

    // rowsAffected is the number of items removed from cache
    int rowsAffected = cache.getSearchService().executeNonQuery(queryCommand);
}
catch (OperationFailedException ex)
{
    if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures 
        // Operation Timeout
        // Operation performed during state transfer    
    }
}
catch (Exception ex)
{
    // Any generic exception like IllegalArgumentException or NullPointerException  
}
try {
    // Pre-condition: Cache is already connected
    // Items are already present in the cache with tags
    // Custom class is query indexed through NCache Web Manager or config.ncconf

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

    // Use QueryCommand for query execution
    val queryCommand = new QueryCommand(query)
    queryCommand.setParameters(Map("$Tag$" -> "Important Customers"))

    // rowsAffected is the number of items removed from cache
    val rowsAffected = cache.getSearchService.executeNonQuery(queryCommand)
}
catch {
    case exception: Exception => {
      // Handle any errors
    }
}
// This is an async method
try
{
    // Pre-condition: Cache is already connected
    // Items are already present in the cache with tags
    // Custom class is query indexed through NCache Web Manager 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);
}
catch(error)
{
    // Handle errors
}
try:
    # Pre-condition: Cache is already connected
    # Items are already present in the cache with tags
    # Custom class is query indexed through NCache Web Manager 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)
except Exception as exp:
    # Handle errors
Warning

Providing 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.

In order to get more detail on object queries please refer to the SQL Reference for NCache section.

Additional Resources

NCache provides sample application for Tags on GitHub.

See Also

Using Groups
Add/Update Data in Cache with Tags
Retrieve Cache Data with Tags
Remove Cache Data with Tags
Search Tag Data in Cache with SQL
Named Tags

Back to top Copyright © 2017 Alachisoft