Search code examples
javacachinghazelcasthazelcast-imap

Is it possible to remove all data from HazelcastInstance (maps & lists) at once?


I have multiple junit tests which use the same HazelcastInstance.

I have a @After method to delete all maps, lists manually one by one.

Is there a way to reset data in HazelcastInstance including IMap, ReplicatedMap & IList so that I can make one call only to reset my cache?

Here is what I am doing right now:

@After
public void afterEachTest() {
    hazelcastInstance.getReplicatedMap(MAP_NAME_A).clear();
    hazelcastInstance.getReplicatedMap(MAP_NAME_B).clear();
    hazelcastInstance.getReplicatedMap(MAP_NAME_C).clear();
    hazelcastInstance.getMap(MAP_NAME_D).clear();
    hazelcastInstance.getList(MAP_NAME_E).clear();
}

Thanks in advance!


Solution

  • That will probably do what you need.

                hazelcast.getDistributedObjects()
                         .forEach(distributedObject -> distributedObject.destroy());
    

    Additionally, if you already have a reference to distributed objects, you can still use them without reinitializing like replicatedMap = hazelcastInstance.getReplicatedMap(MAP_NAME_A)