Delete fieldgroup when deleting plugin 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'        =&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)
  );
}


I've been searching the docs to see what actually happens when a plugin is being deleted, but can't find anything. Any ideas?
You can see what Craft does when uninstalling a plugin by looking at craft/app/services/PluginsService's uninstallPlugin 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.