Deprecated Annotation

Use the deprecated annotation to identify methods, classes, exceptions, enums, interfaces, or variables that can no longer be referenced in subsequent releases of the managed package in which they reside. This is useful when you are refactoring code in managed packages as the requirements evolve. New subscribers cannot see the deprecated elements, while the elements continue to function for existing subscribers and API integrations.

The following code snippet shows a deprecated method. The same syntax can be used to deprecate classes, exceptions, enums, interfaces, or variables.

  @deprecated
  // This method is deprecated. Use myOptimizedMethod(String a, String b) instead.
  global void myMethod(String a) {
   
}

Note the following rules when deprecating Apex identifiers:

  • Unmanaged packages cannot contain code that uses the deprecated keyword.
  • When an Apex item is deprecated, all global access modifiers that reference the deprecated identifier must also be deprecated. Any global method that uses the deprecated type in its signature, either in an input argument or the method return type, must also be deprecated. A deprecated item, such as a method or a class, can still be referenced internally by the package developer.
  • webService methods and variables cannot be deprecated.
  • You can deprecate an enum but you cannot deprecate individual enum values.
  • You can deprecate an interface but you cannot deprecate individual methods in an interface.
  • You can deprecate an abstract class but you cannot deprecate individual abstract methods in an abstract class.
  • You cannot remove the deprecated annotation to undeprecate something in Apex after you have released a package version where that item in Apex is deprecated.

For more information about package versions, see What is a Package?.

Previous
Next