<p>You can see what Craft does when uninstalling a plugin by looking at <code>craft/app/services/PluginsService</code>'s <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/PluginsService.php#L442" rel="nofollow noreferrer"><code>uninstallPlugin</code></a> method.</p>

<p>Craft will delete any custom elements the plugin has defined from <code>craft_elements</code> and the cascading deletes across foreign keys to that table will trigger.  It will drop any tables defined by any plugin records. It will remove the plugin row from the <code>craft_plugins</code> table and any migrations from the <code>craft_migrations</code> table.</p>

<p><code>craft_field</code> and <code>craft_fieldgroups</code> won't get picked up because they don't have any indirect FK connection to <code>craft_elements</code>, but you can add an <code>onBeforeUninstall</code> method in your base plugin class and do any custom cleanup you want there.</p>

CC BY-SA 3.0
2017-02-03T09:54:16.450Z
Thanks for your reply. You mention `craft_field` and `craft_fieldgroups` won't get picked up. How would someone handle those when writing a plugin? Currently when a plugin is uninstalled and reinstalled, the same name can't be used. I don't think this is great behaviour. Do you have any suggestions on improving this on my side?
sanderfish
CC BY-SA 3.0
2017-02-03T00:48:02.990Z
2017-02-03T00:48:02.990Z
Brad Bell
<p>I'm making a plugin that stores elements in fields and fieldGroups. When deleting one the elements with  the fields/field groups are being removed too. </p>

<p>But when deleting the whole plugin, those fields are still in the database. When reinstalling the plugin this makes it impossible to add the field with the same title again.</p>

<p>I've already added 'onDelete' things in all the records like this:</p>

<pre><code>public function defineRelations()
{
  return array(
    'form'        =&gt; array(static::BELONGS_TO, 'PluginHandle_FormRecord', 'onDelete' =&gt; static::CASCADE),
    'fieldGroup'  =&gt; array(static::BELONGS_TO, 'FieldGroupRecord', 'onDelete' =&gt; static::CASCADE),
    'field'       =&gt; array(static::BELONGS_TO, 'FieldRecord', 'onDelete' =&gt; static::CASCADE)
  );
}
</code></pre>

<p>I've been searching the docs to see what actually happens when a plugin is being deleted, but can't find anything. Any ideas?</p>

CC BY-SA 3.0
2017-02-03T10:36:25.910Z
You're assuming that someone hasn't added extra fields to the group you want to delete?
darylknight
CC BY-SA 3.0
2017-02-03T10:47:36.597Z
Am I? The base of the plugin is a formbuilder. When someone uninstalls the plugin, I want to delete all the created forms (and it's belongings) with it, so when the person reinstalls there won't be any conflicts w/ existing fields and fieldgroups in the database.
sanderfish
<plugin-development><plugins><field>
Delete fieldgroup when deleting plugin
# Delete fieldgroup when deleting plugin

(Asked by: username_0 on 2017-02-02)

I'm making a plugin that stores elements in fields and fieldGroups. When deleting one the elements with  the fields/field groups are being removed too. 
But when deleting the whole plugin, those fields are still in the database. When reinstalling the plugin this makes it impossible to add the field with the same title again.
I've already added 'onDelete' things in all the records like this:
public function defineRelations()
{
  return array(
    'form'        => array(static::BELONGS_TO, 'PluginHandle_FormRecord', 'onDelete' => static::CASCADE),
    'fieldGroup'  => array(static::BELONGS_TO, 'FieldGroupRecord', 'onDelete' => static::CASCADE),
    'field'       => array(static::BELONGS_TO, 'FieldRecord', 'onDelete' => static::CASCADE)
  );
}

I've been searching the docs to see what actually happens when a plugin is being deleted, but can't find anything. Any ideas?

**Tags:** plugin-development, plugins, field

> **Comments:**
> **username_1**: You're assuming that someone hasn't added extra fields to the group you want to delete?  
> **username_0**: Am I? The base of the plugin is a formbuilder. When someone uninstalls the plugin, I want to delete all the created forms (and it's belongings) with it, so when the person reinstalls there won't be any conflicts w/ existing fields and fieldgroups in the database.  

---

## 1 answers

### Answer by username_2 on 2017-02-03. Score: 1

You can see what Craft does when uninstalling a plugin by looking at `craft/app/services/PluginsService`'s [`uninstallPlugin`](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/PluginsService.php#L442) method.

Craft will delete any custom elements the plugin has defined from `craft_elements` and the cascading deletes across foreign keys to that table will trigger. It will drop any tables defined by any plugin records. It will remove the plugin row from the `craft_plugins` table and any migrations from the `craft_migrations` table.

`craft_field` and `craft_fieldgroups` won't get picked up because they don't have any indirect FK connection to `craft_elements`, but you can add an `onBeforeUninstall` method in your base plugin class and do any custom cleanup you want there.

> **Comments:**
> **username_0**: Thanks for your reply. You mention `craft_field` and `craft_fieldgroups` won't get picked up. How would someone handle those when writing a plugin? Currently when a plugin is uninstalled and reinstalled, the same name can't be used. I don't think this is great behaviour. Do you have any suggestions on improving this on my side?  

---

