// Module included in the following assemblies:
//
// assembly-using-the-topic-operator.adoc

[id='con-deleting-managed-topics-{context}']
= (Preview) Deleting managed topics

[role="_abstract"]
Unidirectional topic management supports the deletion of topics managed through the `KafkaTopic` resource with or without Kubernetes finalizers.
This is controlled by the `STRIMZI_USE_FINALIZERS` Topic Operator environment variable.
By default, this is set to `true`, though it can be set to `false` in the Topic Operator `env` configuration if you do not want the Topic Operator to add finalizers.

NOTE: Unidirectional topic management is available as a preview. 
Unidirectional topic management is not enabled by default, so you must xref:ref-operator-unidirectional-topic-operator-feature-gate-{context}[enable the `UnidirectionalTopicOperator` feature gate] to be able to use it.

Finalizers ensure orderly and controlled deletion of `KafkaTopic` resources.
A finalizer for the Topic Operator is added to the metadata of the `KafkaTopic` resource:

.Finalizer to control topic deletion
[source,shell,subs="+quotes"]
----
apiVersion: {KafkaTopicApiVersion}
kind: KafkaTopic
metadata:
  generation: 1
  name: my-topic-1
  finalizer: 
    strimzi.io/topic-operator
  labels:
    strimzi.io/cluster: my-cluster
----

In this example, the finalizer is added for topic `my-topic-1`.
The finalizer prevents the topic from being fully deleted until the finalization process is complete.
If you then delete the topic using `kubectl delete kafkatopic my-topic-1`, a timestamp is added to the metadata:

.Finalizer timestamp on deletion
[source,shell,subs="+quotes"]
----
apiVersion: {KafkaTopicApiVersion}
kind: KafkaTopic
metadata:
  generation: 1
  name: my-topic-1
  finalizer: 
    strimzi.io/topic-operator
  labels:
    strimzi.io/cluster: my-cluster
  deletionTimestamp: 20230301T000000.000  
----

The resource is still present.
If the deletion fails, it is shown in the status of the resource.

When the finalization tasks are successfully executed, the finalizer is removed from the metadata, and the resource is fully deleted.  

Finalizers also prevent related resources from being deleted. 
If the unidirectional Topic Operator is not running, it won't be able to remove the `metadata.finalizer`. 
Consequently, an attempt to delete the namespace that contains the `KafkaTopic` resource won't complete until either the operator is restarted, or the finalizer is otherwise removed (for example using `kubectl edit`). 