Try Playground
Show / Hide Table of Contents

Delete Data with ExecuteNonQuery

NCache lets you delete the data from the cache based on the given criteria. The Delete statement returns the number of the deleted rows through the query executed using ExecuteNonQuery.

Important

The Delete statement can only be executed through ExecuteNonQuery.

Prerequisites to Delete Data with ExecuteNonQuery

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
  • 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.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand, ExecuteNonQuery.
  • 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.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService, QueryCommand, executeNonQuery.
  • 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.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, execute_reader, get_search_service, QueryCommand, execute_non_query.
  • 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.
  • Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService, QueryCommand, executeNonQuery.
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Ensure the searchable objects and their attributes are indexed and added in the cache using the basic NCache API.
  • Include the Alachisoft.NCache.Web.Caching namespace in your application.

Here's an example that deletes all the products from the cache with the UnitsInStock equals to 0 using ExecuteNonQuery.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// Precondition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
string query = "DELETE FROM FQN.Product WHERE UnitsInStock = ?";

// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("UnitsInStock", 0);

// Execute query
_cache.SearchService.ExecuteNonQuery(queryCommand);
// Precondition: Cache is already connected
// Items are already present in the cache

// Provide Fully Qualified Name (FQN) of your custom class
String query = "DELETE FROM FQN.Product WHERE UnitsInStock = ?";

// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("UnitsInStock", 0);

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

System.out.println(affectedRows + " rows were affected by the delete operation.");
# Precondition: Cache is already connected
# Items are already present in the cache
# Provide Fully Qualified Name(FQN) of your custom class
query = "DELETE FROM FQN.Product WHERE units_in_stock = 0"

# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)

# Execute query
cache.get_search_service().execute_non_query(query_command)
// This is an async method
// Precondition: Cache is already connected
// Items are already present in the cache
// Provide Fully Qualified Name (FQN) of your custom class
var query = "DELETE FROM FQN.Product WHERE UnitsInStock = ?";

// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);

//Providing parameters for query
let parameter = new Map();
parameter.set("UnitsInStock", 500);
queryCommand.setParameters(parameter);

// Execute query
await ( await this.cache.getSearchService()).executeNonQuery(queryCommand);
string query = "DELETE Product WHERE this.ProductID > ?";
Hashtable values = new Hashtable();
values.Add("ProductID", 5);
int result = cache.ExecuteNonQuery(query, values);
//return number of affected rows.
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Additional Resources

NCache provides a sample application for SQL Queries on GitHub.

See Also

.NET: Alachisoft.NCache.Client.Services namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.client.services class.
Node.js: Cache class.

In This Article
  • Prerequisites to Delete Data with ExecuteNonQuery
  • 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