★ Founding developer program: Pro free for 90 days for the first 50 developers →

Guides · Engineering

How to update your plugin's config.yml without wiping server owners' settings

New options, renamed keys, changed defaults. How to migrate a Bukkit plugin's config.yml on update without losing what server owners set.

Updated · by Skilled · 5 min read

Sooner or later every plugin update touches config.yml. You add an option, rename a key that was badly named, or move three settings into a section. The server owner drops in the new jar, restarts, and one of two things happens: the new option is missing and your code falls back to something odd, or the plugin regenerated the file and their settings are gone.

The second one gets you a review. Both are avoidable with a few lines in onEnable.

Why saveDefaultConfig isn't enough

saveDefaultConfig() copies the config.yml from your jar only when the file doesn't exist yet. On an existing server it does nothing, so new keys never reach the file on disk.

Your code still works, because getConfig() falls back to the defaults inside your jar for any missing key. But the server owner can't see the new option, doesn't know it exists, and can't change it without reading your changelog and typing it in by hand.

Adding new keys

For new options, ask Bukkit to copy the missing defaults into the loaded config and save it:

@Override
public void onEnable() {
    saveDefaultConfig();
    getConfig().options().copyDefaults(true);
    saveConfig();
}

copyDefaults(true) only fills in keys that are missing. Existing values stay as the owner set them. This covers most updates, with two caveats:

  • Comments. Before 1.18.1, Bukkit drops every comment when it saves a config. Newer versions keep them, but check how a new key looks in the saved file before you rely on comments as documentation. A wiki page or a changelog line is safer for explaining new options.
  • Removed keys stay. Keys you no longer use are left in the file. Harmless, but confusing: an owner can change an option that does nothing.

Renaming and moving keys

copyDefaults can't know that max-players became arena.max-players. It adds the new key with your default and leaves the old one, so the owner's value is silently replaced by yours. For renames you need a version number in the config and a migration step.

Put a version at the top of your jar's config.yml:

config-version: 3

Then migrate on startup, one step per version, before anything reads the config:

private static final int CONFIG_VERSION = 3;

private void migrateConfig() {
    FileConfiguration config = getConfig();
    int version = config.getInt("config-version", 1);
    if (version >= CONFIG_VERSION) return;

    backupConfig(version);

    if (version < 2) {
        move(config, "max-players", "arena.max-players");
        move(config, "min-players", "arena.min-players");
    }
    if (version < 3) {
        // 0 used to mean "no limit"; now it's -1
        if (config.getInt("arena.round-seconds") == 0) {
            config.set("arena.round-seconds", -1);
        }
        config.set("legacy-chat", null);
    }

    config.set("config-version", CONFIG_VERSION);
    config.options().copyDefaults(true);
    saveConfig();
    getLogger().info("Updated config.yml from version " + version + " to " + CONFIG_VERSION);
}

private static void move(FileConfiguration config, String from, String to) {
    if (config.isSet(from) && !config.isSet(to)) {
        config.set(to, config.get(from));
    }
    config.set(from, null);
}

Three details matter here:

  • Read with a default of 1. Configs from before you added the version have no config-version, so they're the oldest layout.
  • Chain the steps. A server jumping from version 1 to 3 runs both blocks in order. Never write a step that assumes the config is already on the latest layout.
  • Use isSet, not contains. contains also returns true for keys that only exist in your jar's defaults, which would make every old key look present.

Back up before you change anything

A migration that goes wrong on one server in a thousand is still a server owner who lost their setup. Copy the file before touching it:

private void backupConfig(int version) {
    File file = new File(getDataFolder(), "config.yml");
    File backup = new File(getDataFolder(), "config-v" + version + ".yml.bak");
    try {
        Files.copy(file.toPath(), backup.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        getLogger().warning("Couldn't back up config.yml: " + e.getMessage());
    }
}

Tell the owner in the log line that a backup exists. Someone who sees their values moved will look for it.

Don't overwrite a broken file

When config.yml has a YAML error, usually one bad indent from a manual edit, Bukkit logs it and getConfig() carries on with an empty config backed by your jar's defaults. If your code then saves, the owner's file is replaced by your defaults. Both snippets above save, so check the file loaded before touching it:

private boolean configLoads() {
    File file = new File(getDataFolder(), "config.yml");
    try {
        new YamlConfiguration().load(file);
        return true;
    } catch (IOException | InvalidConfigurationException e) {
        getLogger().severe("config.yml couldn't be read, so it was left as it is and the defaults are in use: " + e.getMessage());
        return false;
    }
}

Call it right after saveDefaultConfig(), and skip the migration and saveConfig() when it returns false. The same goes for the shortcut of deleting the file and writing a fresh one when it fails to parse: it turns a typo into a lost configuration. Leave the file alone and let the owner fix the line the error points to.

Test the migration with real old files

Keep a copy of the default config.yml from every version that had a different layout, under src/test/resources, and load each one in a test to check it ends up with the right values. It's the fastest way to catch a step that assumes something about the layout. If you haven't set up tests for your plugin yet, testing with MockBukkit covers it.

Know which layouts are still out there

Migrations pile up. Every step you keep is code you have to read and test, and at some point you'd like to drop support for version 1 configs. The question is how many servers still have one.

PluginAnalytics reads plain values from your config.yml on each server, so a numeric config-version shows up in Config usage with the share of servers on each value, without extra code. Private keys like passwords, hosts and URLs are never read. The same page shows which options most servers change, which is useful before you rename or remove one. Config usage

Config usage is part of Pro and Premium.

The value in config.yml is what the file says after your migration ran, so it tells you which servers have restarted on the new version. Which plugin versions servers run, and which ones they roll back from, is on the Servers page. A release that servers keep rolling back from right after a config change is worth a closer look. Did your update break something?

Say it in the changelog

Even a perfect migration should be announced. One line is enough: which keys moved, that the old values were carried over, and where the backup is. Owners who edit their config by hand from an old guide will otherwise put the old keys back. Writing a changelog people read

Skilled

Makes Minecraft plugins (including The Sift on SpigotMC) and PluginAnalytics, so other plugin developers can see how their plugins are found, installed and used.

More guides

Product

Which config options do server owners actually change?

Your config.yml has dozens of options. Find out which ones server owners actually change, fix the defaults everyone changes, and read nothing private.

4 min read
Growth

How to write a plugin changelog that server owners actually read

Server owners decide whether to update from your changelog in under a minute. What to put first, how to write breaking changes, and where to post it.

5 min read

See how your own plugin is found and used

Free for your first plugin or mod. One line of code.