< API16:JInstallerComponent

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


Custom uninstall method for components

<! removed transcluded page call, red link never existed >


Parameter Name Default Value Description
$id $id The unique extension id of the component to uninstall


mixed Return value for uninstall method in component uninstall file

Defined in



jimport( 'joomla.installer.adapters.component' );

Source Body

public function uninstall($id)
        // Initialise variables.
        $db = &$this->parent->getDbo();
        $row    = null;
        $retval = true;

        // First order of business will be to load the component object table from the database.
        // This should give us the necessary information to proceed.
        $row = & JTable::getInstance('extension');
        if (!$row->load((int) $id)) {
                JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
                return false;

        // Is the component we are trying to uninstall a core one?
        // Because that is not a good idea...
        if ($row->protected) {
                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::sprintf('WARNCORECOMPONENT', $row->name)."<br />".JText::_('WARNCORECOMPONENT2'));
                return false;

        // Get the admin and site paths for the component
        $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR.DS.'components'.DS.$row->element));
        $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE.DS.'components'.DS.$row->element));
        $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator')); // copy this as its used as a common base

        // Attempt to load the admin language file; might have uninstall strings
        $lang = &JFactory::getLanguage();
        // 1.6
        $lang->load($row->element, $this->parent->getPath('extension_administrator'));
        // 1.5 or Core

        // Find and load the XML install file for the component
        $this->parent->setPath('source', $this->parent->getPath('extension_administrator'));

        // Get the package manifest object
        $this->manifest = $this->parent->getManifest();
        if ( ! $this->manifest)
                // Make sure we delete the folders if no manifest exists

                // Remove the menu

                // Raise a warning
                JError::raiseWarning(100, JText::_('ERRORREMOVEMANUALLY'));

                // Return
                return false;

        // Set the extensions name
        $name = strtolower(JFilterInput::getInstance()->clean((string)$this->manifest->name, 'cmd'));
        $check=substr($name, 0, 4);
        if ($check="com_") {
        $name = substr($name, 4); }
        $this->set('name', $name);

        // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
        $scriptFile = (string)$this->manifest->scriptfile;
        if ($scriptFile)
                $manifestScriptFile = $this->parent->getPath('source').DS.$scriptFile;
                if (is_file($manifestScriptFile))
                        // load the file
                        include_once $manifestScriptFile;
                // Set the class name
                $classname = $row->element.'InstallerScript';
                if (class_exists($classname))
                        // create a new instance
                        $this->parent->manifestClass = new $classname($this);
                        // and set this so we can copy it later
                        $this->set('manifest_script', $scriptFile);
                        // Note: if we don't find the class, don't bother to copy the file

        // run uninstall if possible
        if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'uninstall')) $this->parent->manifestClass->uninstall($this);
        $msg = ob_get_contents();

        // Now lets load the uninstall file if there is one and execute the uninstall function if it exists.
        $uninstallFile = (string)$this->manifest->uninstallfile;
        if ($uninstallFile)
                // Element exists, does the file exist?
                if (is_file($this->parent->getPath('extension_administrator').DS.$uninstallFile))
                        require_once $this->parent->getPath('extension_administrator').DS.$uninstallFile;
                        if (function_exists('com_uninstall'))
                                if (com_uninstall() === false)
                                        JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('Custom Uninstall script unsuccessful'));
                                        $retval = false;
                        $msg .= ob_get_contents(); // append this in case there was something else

        if ($msg != '') {
                $this->parent->set('extension_message', $msg);

         * Let's run the uninstall queries for the component
         *      If Joomla 1.5 compatible, with discreet sql files - execute appropriate
         *      file for utf-8 support or non-utf support
        // try for Joomla 1.5 type queries
        // second argument is the utf compatible version attribute
        $utfresult = $this->parent->parseSQLFiles($this->manifest->uninstall->sql);
        if ($utfresult === false)
                // Install failed, rollback changes
                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
                $retval = false;


        // Let's remove language files and media in the JROOT/images/ folder that are
        // associated with the component we are uninstalling
        $this->parent->removeFiles($this->manifest->administration->languages, 1);

        // Clobber any possible pending updates
        $update = &JTable::getInstance('update');
        $uid = $update->find(Array('element'=>$row->element,
        if ($uid) $update->delete($uid);

        // Now we need to delete the installation directories.  This is the final step in uninstalling the component.
        if (trim($row->element))
                // Delete the component site directory
                if (is_dir($this->parent->getPath('extension_site')))
                        if (!JFolder::delete($this->parent->getPath('extension_site')))
                                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('UNABLE_TO_REMOVE_THE_COMPONENT_SITE_DIRECTORY'));
                                $retval = false;

                // Delete the component admin directory
                if (is_dir($this->parent->getPath('extension_administrator')))
                        if (!JFolder::delete($this->parent->getPath('extension_administrator')))
                                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('UNABLE_TO_REMOVE_THE_COMPONENT_ADMIN_DIRECTORY'));
                                $retval = false;

                // Now we will no longer need the extension object, so lets delete it and free up memory
                unset ($row);

                return $retval;
                // No component option defined... cannot delete what we don't know about
                JError::raiseWarning(100, 'Component Uninstall: Option field empty, cannot remove files');
                return false;

<! removed transcluded page call, red link never existed >


Code Examples