Removing topics

Typically, applications remove topics that represent a transient stream of data, such as an active user’s location, where the latest value is no longer needed.

When a topic is removed, all subscribers receive a notification that the topic has been removed, however they remain subscribed. Hence, if the topic is recreated, they will continue to receive updates without having to re-subscribe.

Removing individual topics

To remove an individual topic:

Java and Android
session.feature(TopicControl.class).removeTopics("my/topic/path");
JavaScript
await session.topics.remove('my/topic/path');
console.log('Topics has been removed');
.NET
await session.TopicControl.RemoveTopicsAsync("my/topic/path");
WriteLine($"Topic has been removed!");
C
static int on_topic_removed(
        SESSION_T *session,
        const DIFFUSION_TOPIC_REMOVAL_RESULT_T *result,
        void *context)
{
        return HANDLER_SUCCESS;
}


...


TOPIC_REMOVAL_PARAMS_T remove_params = {
        .topic_selector = "my/topic/path",
        .on_removed = on_topic_removed
};

topic_removal(session, remove_params);
Python
await session.topics.remove_topic("my/topic/path")
print("Topic has been removed")
Apple
session.topicControl.removeTopics(withTopicSelectorExpression: "my/topic/path") { (result, error) in
    print("Topic has been removed!")
}

Removing topics with selectors

You can also remove topics with topic selectors. This allows you to easily remove large parts of the topic space with a single request.

To remove topics with selectors:

Java and Android
session.feature(TopicControl.class).removeTopics(
    Diffusion.topicSelectors().parse("?my/topic//"));
JavaScript
const result = await session.topics.remove('?my/topic//');
console.log(`${result.removedCount} topics have been removed`);
.NET
await session.TopicControl.RemoveTopicsAsync("?my/topic//");
WriteLine($"Topics that matched the topic selector have been removed!");
C
static int on_topic_removed_with_selector(
        SESSION_T *session,
        const DIFFUSION_TOPIC_REMOVAL_RESULT_T *result,
        void *context)
{
        return HANDLER_SUCCESS;
}


...


TOPIC_REMOVAL_PARAMS_T remove_params = {
        .topic_selector = "?my/topic//",
        .on_removed = on_topic_removed_with_selector
};

topic_removal(session, remove_params);
Python
await session.topics.remove_topic("?my/topic//")
print("Topics that matches the topic selector have been removed!")
Apple
session.topicControl.removeTopics(withTopicSelectorExpression: "?my/topic//") { (result, error) in
    print("Topics that matched the topic selector have been removed!")
}