# Drupal 8 – Differences between Configuration API & State API

> The other day, I faced a serious problem. On importing the configuration on my production environment. all the configurations files saved for my module were overridden by the configuration on \[…\]

Source: https://antistatique.net/en/blog/drupal-8-differences-between-configuration-api-state-api

Category: Development

# Drupal 8 – Differences between Configuration API & State API

Author(s)

[

![Kevin](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Kevin-300x300.jpg)



](/en/agence/kevin)

[Kevin](/en/agence/kevin)14 June 2016

The other day, I faced a serious problem. On importing the configuration on my production environment. all the configurations files saved for my module were overridden by the configuration on my development environments.

What I did was very simple to understand:

-   The customer change his configuration on the website, such as API Key of Campaignmonitor,
-   I create a new feature, push it on production and I use the `drush cex` command to export all the configuration on development env,
-   I use the `drush cim` command to import the configuration on my production env,
-   The web site contain now the configuration of my development environments because every configuration is exported/imported on the Configuration API.

A solution could be to use the same configuration on the production & development environments. But it's not possible in our case, we should sync the local database with the production one - it's against productivity.

![Upside down the astronaut](https://antistatique.net/sites/default/files/large-article/drupal8-configuration-api-state-api-upside-down.png)

On my old days with Drupal 7 I used to use:

```
$data = variable_set('test.content_types'. 'value');
```

and port this to Drupal 8 using:

```
\Drupal::config('test')->set('content_types', 'value');
```

That's fine and it works but later when I run a drush config-import `drush cim` on my production server, it erase (reset) the specific configuration of my production server.

**Configuration** is a place to **store information** that you want to **synchronize from development to production**. This information is often created during site building and is not typically generated by regular users during normal site operation.

You should use the **State API**, not Configuration API, for **storing local variables that shouldn't travel between instances**.

## Typical usage of State API

**Get a value:**

```
$val = \Drupal::state()->get('key');
```

**Get multiple key/value pairs:**

```
$pairs = \Drupal::state()->getMultiple($keys);
```

**Set a value:**

```
\Drupal::state()->set('key','value');
```

**Set multiple values:**

```
\Drupal::state()->setMultiple($keyvalues);
```

**Delete a value:**

```
\Drupal::state()->delete('key');
```

## Typical usage of Configuration API

**Retrieve configuration as readonly:**

```
$config = \Drupal::config('mymodule.foo');
```

**Get a value:**

```
$val = $config->get('key');
```

The configuration object that was obtained and used in the previous examples does not allow you to change configuration. If you want to change configuration, you will instead need to get the configuration object by making a call to getEditable() on the configuration factory:

```
$config =\Drupal::service('config.factory')->getEditable('mymodule.foo');
```

**Set a value:**

```
$config->set('enabled', 1);
// Save the configuration
$config->save();
```

**Delete a value:**

```
$config->clear('bar.boo')->save();
```

State information differs from configuration in the following ways:

-   It is specific to an individual environment.
-   You will never deploy to deploy it between environments.
-   You can reset a system, losing all state. Its configuration remains.

We try out a lot of other solution whitout success:

-   Using the flags `--skip-module` or `--partial` of `drush cim`
-   Hooking the Event like `ConfigEvents::DELETE` and the `$event->stopPropagation();`

**Currently it is the most elegant solution we found to solve our problem. However, opinions differ greatly within the community about this use case of State API.**

And you what do you think ?

![What is next?](https://antistatique.net/sites/default/files/large-article/drupal8-configuration-api-state-api-next.png)

More information about:

-   [Drupal 7 - Variable Set](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/variable_set/7.x)
-   [Drupal 7 - Variable get](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/variable_get/7.x)
-   [Drupal 8 - Sate API](https://www.drupal.org/developing/api/8/state)
-   [Drupal 8 - Configuration API](https://www.drupal.org/developing/api/8/state)

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
