Conditions.
# The Controller is implemented so that it stores all the data used and created during the application instance creating, reconfiguring, and upgrading, excluding the data related to the installation-only settings. Also, the Controller stores the application metadata and scripts at the instance-specific locations.
# The application has only one service.
Starting Point. User initiates removing application instance from a particular hosting environment.
Step 1. The Controller disables access to application instance and removes application instance URL mapping.
Step 2. The Controller executes service configuration script.
Sub-step 2.1. From the application metadata stored on the site, the Controller gets information about configuration script language ("/application/service/provision/configuration-script/script-language"), and checks if there is an appropriate interpreter in the system and if it can be used on the environment to run the configuration script.
Sub-step 2.2. Gets all the application data created during the instance provisioning, upgrades and changes, such as the settings data (excluding installation-only settings), data about resources allocated to the instance and other changes made by the Control Panel to the system (web server configuration, permissions, etc.). Basing on this data, the Controller creates environment variables as defined by the Specification.
Sub-step 2.3. Using the interpreter, the Controller runs the configuration script with the remove argument, and passes to it all the environment variables it created on the previous sub-step.
Sub-step 2.4. If the script invocation fails, the Controller returns an error, stops removing application and displays to the user the error message. Otherwise, it proceeds to the next step.
Step 3. The Controller removes the application instance files and folders.
Sub-step 3.1. Gets the information on the application files and folders copied from the application package to the site during the installation. Basing on this information, the Controller creates two file trees, one representing the files and folders of application itself, another the files and folders created with the application instance.
Sub-step 3.2. In the form of a dialogue screen, the Controller asks the user whether he wants to remove all files related to the application instance, or remove only application files and keep the files created by the application instance.
Sub-step 3.3. Depending on the user's choice, the Controller removes either all instance-related files and folders, or only those of the application itself.
Step 4. The Controller rolls back the changes made to the system with the purpose of the instance proper functioning.
Sub-step 4.1. Retrieves information about the resources allocated to the application instance, and frees them.
Sub-step 4.2. Retrieves information about other changes made to the system (e.g., web server configuration), and rolls them back.