Is there a correct way to remove a setting?

Is there a correct way to remove - i.e., not update, but actually remove - a setting in the settings Map?

I have a multi-page custom application. In one page-navigation scenario, a user leverages the browser's back button and makes a change that causes one previously-entered setting to no longer apply.

While I can write the code to ignore the user's prior variable entry, my preference is to back out the input variable if there is a correct way to do so.

My FAILED Brute-Force Removal Attempt

settings = settings.findAll { it.key != 'X' }   // Where "X' is the key being backed out.

**Issue #1 (post attempted removal) **

The following discrepancy appeared "in the logs" AND on a user-input summary page (with data populated using "paragraph").

settings['X']     // reports null
settings.X        // reports null
X                 // reports prior value NOT null 

Issue #2 (post attempted removal AND page refresh)

When I refreshed my summary page (after observing Issue #1), the "removed" KEY reappeared. Both the logs AND the summary page showed the "removed" KEY with its prior value.

You need to use removeSetting(). See: InstalledApp Object | Hubitat Documentation

The settings object itself should not be directly manipulated (same if you need to update a value).

In your case, something like this might work:

def settingNamesToRemove = settings.keySet()?.findAll { it != "x" }
settingNamesToRemove.each {
      app.removeSetting(it)
}
2 Likes

Perfect. Thanks!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.