Let the platform do the work

Removing Files with an Installable Package

Overview

This is an overview of how to create a module loadable package that will remove files from the custom directory that may have been left over from a previous package installation or legacy versions of your code customization. 

Note: For SugarCloud customers, this method will not work because removing files is prohibited by Package Scanner. To have files removed, you can submit a case to Sugar Support with a list of files to be removed or request a package approval and provide your module loadable package.

This example will install a custom file and use a pre-execute script to remove files from a previously installed customization. Typically, uninstalling a package would remove the files of the customization, however, in some scenarios, it is desired to install a new package on top of a previous version, and removing files with the new package might be necessary. The full package for this example is downloadable here for your reference.

For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest documentation. 

Manifest Example

<basepath>/manifest.php

  <?php

    $manifest = array(
        'acceptable_sugar_flavors' => array(
            'PRO',
            'ENT',
            'ULT'
        ),
        'acceptable_sugar_versions' => array(
            'exact_matches' => array(),
            'regex_matches' => array(
                '13.0.*
            '),
        ),
        'author' => 'SugarCRM',
        'description' => 'Installs a custom class file',
        'icon' => '',
        'is_uninstallable' => true,
        'name' => 'Example Removing File Installer',
        'published_date' => '2018-12-06 00:00:00',
        'type' => 'module',
        'version' => '1.2.0'
    );

    $installdefs = array(
        'id' => 'package_1341607504',
        'copy' => array(
            array(
                'from' => '<basepath>/Files/custom/src/CustomClass.php',
                'to' => 'custom/src/CustomClass.php',
            ),
        ),
        'pre_execute' => array(
            '<basepath>/removeLegacyFiles.php',
        ),
        'remove_files' => array(
            'custom/include/CustomClass.php'
        )
    );

<basepath>/removeLegacyFiles.php 

  <?php

if (isset($this->installdefs['remove_files'])) {
    foreach($this->installdefs['remove_files'] as $relpath){
        if (SugarAutoloader::fileExists($relpath)) {
            SugarAutoloader::unlink($relpath);
        }
    }
}

The full package is downloadable here for your reference.

Topics