Try Playground
Show / Hide Table of Contents

Remove Tags in Data Cache

The items that are added to the data cache with particular tags can also be removed from the data cache using the same tags. The user can remove tagged items along with their values from the data cache.

Prerequisites for Removing Tags in Data Cache

  • .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.
  • For API details, refer to: ICache, Tag, RemoveByTag, RemoveByTags, RemoveBulk, 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.
  • For API details, refer to: Cache, Tag, removeByTag, removeByTags, removeBulk, 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.
  • For API details, refer to: Cache, remove_by_tag, remove_bulk, remove_by_tags, 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.
  • For API details, refer to: Cache, Tag, removeByTag, removeByTags, removeBulk, getSearchService.
  • Create new console application.
  • Include the following namespace in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Runtime.Exceptions
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Remove Tagged Data from Cache

Similar to retrieving items using tags, tagged data can be removed using either the RemoveByTag or RemoveByTags method containing TagSearchOptions (ByAllTags and ByAnyTag). These methods are then explained below.

Note

These three APIs do not return anything after removal.

Remove By Tag

RemoveByTag is a method in which a single tag is provided and the items associated with that particular tag are removed from the cache.

The following example removes the cache items associated with the tag East Coast Customers.

Tip

One quick way to verify the successful removal of items from the cache is to check the item count by using the Count property of the cache before and after operating.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// Precondition: Cache is already connected

// Create a tag object
Tag tag = new Tag("East Coast Customers");

// Cache items containing the tag 'East Coast Customers' are removed from the cache
cache.SearchService.RemoveByTag(tag);
// Pre-condition: Cache is already connected

// Create a tag object
Tag tag = new Tag("East Coast Customers");

// Cache items containing the tag 'East Coast Customers' are removed from the cache
cache.getSearchService().removeByTag(tag);
System.out.println("Items with tag 'East Coast Customers' removed successfully");
# Precondition: Cache is already connected
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Cache items containing the tag 'Important Customers' are removed from the cache
search_service = cache.get_search_service()
keys = search_service.remove_by_tag(tags[0])
// Precondition: Cache is already connected
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Cache items containing the tag 'Important Customers' are removed from the cache

var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTag(tags[0]);
// Pre-condition: Cache is already connected

// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");

// Cache items containing the tag 'Important Customers' are removed from the cache
cache.SearchService.RemoveByTag(tags[0]);

// In order to verify successfull removal use cache.Count
Note

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

Remove By Any Tag

RemoveByTags is a method and ByAnyTag is the search option in which a tag list of multiple tags is provided and the items containing ANY of the tags are removed from the cache.

The following example removes the items from the cache containing any tag from the list tags.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// Create an array of tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");

// Cache items containing any of the tags are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAnyTag);
// Create an array of tags to search
List<Tag> tags = List.of(
        new Tag("East Coast Customers"),
        new Tag("West Coast Customers")
);

// Cache items containing any of the tags are removed from the cache
cache.getSearchService().removeByTags(tags, TagSearchOptions.ByAnyTag);
System.out.println("Items with either 'West Coast Customers' or 'East Coast Customers' tags removed successfully");
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Cache items containing the tag 'Important Customers' or 'East Coast Customers' are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, ncache.TagSearchOptions.BY_ANY_TAG)
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Cache items containing the tag 'Important Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTags(tags, ncache.TagSearchOptions.ByAnyTags);
// Pre-condition: Cache is already connected

// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");

// Cache items containing any of the tags are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAnyTag);

// In order to verify successfull removal use the Count method

Remove By All Tags

RemoveByTags is a method and ByAllTags is the search option in which a list of tags can be provided and the items containing all the tags provided in the list will be removed from the cache.

The following example removes the items from the cache containing all tags from the list tags.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// User wants to remove all the customers from the East and West Coast Region
// Cache items containing all of the tags are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAllTags);
// Create an array of tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");

// Cache items containing all of the tags are removed from the cache
cache.getSearchService().removeByTags(List.of(tags), TagSearchOptions.ByAllTags);
System.out.println("Items with both 'West Coast Customers' and 'East Coast Customers' tags removed successfully");
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Cache items containing the tag 'Important Customers' and 'East Coast Customers' are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, ncache.TagSearchOptions.BY_ALL_TAGS)
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Cache items containing the tag 'Important Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTags(tags, ncache.TagSearchOptions.ByAllTags);
// Pre-condition: Cache is already connected

// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");

// Cache items containing the tag 'Important Customers' are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAllTags);

// In order to verify successfull removal use the Count method
Warning

Providing Null tag array will throw an ArgumentNullException or NullPointerException.

Additional Resources

NCache provides 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 for Removing Tags in Data Cache
  • Remove Tagged Data from Cache
    • Remove By Tag
    • Remove By Any Tag
    • Remove By All Tags
  • 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