---
title: "DoNotRetain"
description: "Annotates that a class should not be retained."
type: "class"
lastmod: "2026-07-02T02:32:45.048056Z"
---
## API Reference

> Source set: Common

```kotlin
@MustBeDocumented
@Target(AnnotationTarget.CLASS)
public annotation class DoNotRetain(
    /**
     * An optional explanation of why this class should not be retained. If this value is not an
     * empty string, it will be presented in the error description at the callsite of offending
     * usages of this type.
     */
    val explanation: String = ""
)
```

Annotates that a class should not be retained. Types annotated with [DoNotRetain](/jetpack-compose/androidx.compose.runtime/runtime-annotation/classes/DoNotRetain) should not be
used in the return type of the `calculation` lambda given to the `retain {}` method in
`androidx.compose.runtime`, or as a key input to `retain`.

When present on a class, a lint error will be reported when the class or any of its subclasses
appear as the generic type of a call to `retain` or as a `key` parameter. The associated
inspection only checks the direct return type of the `calculation` lambda, but retained values
should hold no strong references, direct or indirect, to any type marked as [DoNotRetain](/jetpack-compose/androidx.compose.runtime/runtime-annotation/classes/DoNotRetain).

Generally, this annotation is expected to be used on resources that will be leaked if they are
retained. Additional or other rationale may be provided by specifying an [explanation](#explanation), which
will appear in the lint error message when applicable.
