> For the complete documentation index, see [llms.txt](https://wp-launchpad.gitbook.io/launchpad/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wp-launchpad.gitbook.io/launchpad/tutorials/index/index-1/deactivate.md).

# Deactivation

The deactivate logic is some logic executed each time the plugin got disabled.

It is possible to use that state to interrupt the plugin:

* By cancelling crons.
* By updating the settings.

## Create a deactivate logic

In Launchpad, to create a logic to be executed when the plugin disabling, we need to create a deactivator.

To create a deactivator, any class can be used as the only requirement is to [use `@deactivate` annotation](https://github.com/wp-launchpad/launchpad-examples/blob/e77aefdc0133a0fcbeccb96167360411724344a5/deactivate/inc/MyDeactivator.php#L12) inside the docblock of at least one of the methods:

```php
class MyDeactivator {
    /**
    * @deactivate
    */
    public function unregister_options() {
    
    }
}
```

Once the deactivator class is created, it needs to be registered on a service provider to be loaded.

For that it is important to first [add the necessary logic into the provider](https://github.com/wp-launchpad/launchpad-examples/blob/e77aefdc0133a0fcbeccb96167360411724344a5/deactivate/inc/ServiceProvider.php#L9) using [`HasDeactivatorServiceProviderInterface`](https://github.com/wp-launchpad/core/blob/develop/inc/Deactivation/HasDeactivatorServiceProviderInterface.php) interface and [`HasDesactivatorServiceProviderTrait`](https://github.com/wp-launchpad/core/blob/develop/inc/Deactivation/HasDesactivatorServiceProviderTrait.php) trait:

```php
class ServiceProvider extends AbstractServiceProvider implements HasDeactivatorServiceProviderInterface {
	use HasDesactivatorServiceProviderTrait;
	
	/**
	 * Define your services.
	 *
	 * @return void
	 */
	protected function define() {

	}
}
```

It is then possible to have access to the [`register_deactivator` method to register our deactivator](https://github.com/wp-launchpad/launchpad-examples/blob/e77aefdc0133a0fcbeccb96167360411724344a5/deactivate/inc/ServiceProvider.php#L18) inside [the `define` method](https://github.com/wp-launchpad/launchpad-examples/blob/e77aefdc0133a0fcbeccb96167360411724344a5/deactivate/inc/ServiceProvider.php#L17):

```php
class ServiceProvider extends AbstractServiceProvider implements HasDeactivatorServiceProviderInterface {
	use HasDesactivatorServiceProviderTrait;
	
	protected function define() {
		$this->register_deactivator(MyDeactivator::class);
	}
}
```
