I accidentally add a locale, e.g. sudo locale-gen zh\_TW

e.g. locale -a

```
zh_TW

```

But how to remove it?
['https://serverfault.com/questions/394610', 'https://serverfault.com', 'https://serverfault.com/users/50774/']
I think you can do this by editing `/var/lib/locales/supported.d/local` and removing that locale. Then run `locale-gen` to regenerate the compiled locales.
**Note:** Some of the commands below require root privileges, consider the use of `sudo`.

### Basic info

According to [`man locale-gen`](http://manpages.ubuntu.com/manpages/jammy/man8/locale-gen.8.html), locales are set in several files.

> 
> `/etc/locale.gen`
> 
> 
> The main configuration file, which has a simple format: every line that is not empty and does not begin with a # is treated as a locale definition that is to be built.
> 
> 
> `/var/lib/locales/supported.d/`
> 
> 
> A directory containing locale.gen snippets provided by language-pack packages. Do not edit these manually, they will be overwritten on package upgrades.
> 
> 
> 

Locales are compiled (generated) into a single file.

> 
> `/usr/lib/locale/locale-archive`
> 
> 
> Usual default locale archive location.
> 
> 
> 

Comprehensive details on locales at the [Arch Wiki](https://wiki.archlinux.org/index.php/locale).

### Checking locales and the locale

To list available (known) locales, run any of the following commands (with minor output differences).

```bash
locale -a
localectl list-locales

```

To check the (already) generated locales, run the following command.

```bash
localedef --list-archive

```

To check the currently used locale, run any of the following commands (with minor output differences).

```bash
locale
localectl

```

### Setting and generating (new) locales

Locales are typically set by uncommenting lines in `/etc/locale.gen`, after which running `locale-gen` is required.

```bash
nano /etc/locale.gen # uncomment desired lines (locales)
locale-gen

```

This will compile (generate) locales into `/usr/lib/locale/locale-archive` for each uncommented line in `/etc/locale.gen` and under `/var/lib/locales/supported.d/`, whether they were previously compiled or not.

Alternatively, the command

```bash
locale-gen <locale>

```

will uncomment the corresponding line in `locale-gen` while generating the desired locale and only this one.

Note: The implementation of `locale-gen` is distro dependent. For instance, the command above is valid in Ubuntu/Debian but not in ArchLinux.

### Removing locales

When issuing `locale-gen`, the compiled archive is erased and all locales in `/etc/locale.gen` and under `/usr/lib/locale/locale-archive` are regenerated anew. The command `locale-gen --purge <locale>` doesn't do what the modifier suggests but the opposite: It removes all compiled locales except those indicated. To make sure only specific locales are generated when `locale-gen` is issued or and update is performed both `/etc/locale.gen` and `/usr/lib/locale/locale-archive` must be considered.

To remove locales in `/etc/locale.gen`, simply comment the desired lines and regenerate the locales using `locale-gen`.

To remove locales under `/var/lib/locales/supported.d/` is trickier. Since any file `/var/lib/locales/supported.d/<code>` depends on the package `language-pack-<code>-base`, any change on the former will be restored when the latter is updated. To solve this, simply hold the packages that update files under `/var/lib/locales/supported.d/`. The command that achieves this in Ubuntu/Debian is the following.

```bash
apt-mark language-pack-<code>-base

```

To update a held package, you must [unmark](https://manpages.ubuntu.com/manpages/jammy/man8/apt-mark.8.html) it or simply [`--ignore-hold`](https://manpages.ubuntu.com/manpages/jammy/man8/apt-get.8.html).

**Workaround.** A more intrusive but general solution that prevents changes under `/var/lib/locales/supported.d/` is to set files in it with the ["immutable (i)" attribute](http://manpages.ubuntu.com/manpages/jammy/man1/chattr.1.html). So instead of removing files, empty them. For instance:

```bash
cd /var/lib/locales/supported.d/
rm <code> && touch <code> # <code> has been emptied
lsattr <code>    # regular attributes
chattr +i <code> # adding (+) immutable
lsattr <code>    # checking attributes

```

### Setting the locale

Setting and generating locales does not set the system locale. Any of the following commands achieves this.

```bash
echo LANG=<code> | sudo tee /etc/locale.conf # reboot (might be ignored in Ubuntu)
localectl set-locale LANG=<code>

```