Rotate Laravel's APP_KEY Without Treating Every User Like a New User
Changing APP_KEY used to have an unpleasant side effect: encrypted cookies and application data could no longer be decrypted with the new key. That often meant logging users out and losing access to values encrypted under the old key. Laravel 11 added support for a list of previous keys, allowing the application to decrypt older ciphertext while encrypting new values with the current key.
APP_KEY=base64:the-new-key
APP_PREVIOUS_KEYS="base64:the-old-key"
Treat rotation as a deployment, not an environment-variable edit. First, confirm that all instances read the same current and previous key set. Then deploy the new current key together with the old key in APP_PREVIOUS_KEYS. Verify that a known encrypted value can still be read, new values are written successfully, sessions behave as expected, and all instances agree on the configuration. Only remove an old key after the retention window for every encrypted value has passed.
The right retention window depends on what you encrypt. Browser cookies have a different lifetime from encrypted database columns or queued payloads. Make an inventory of encryption consumers before deciding that a key is safe to retire. Keep key material in a secret manager, restrict access, and avoid copying real secrets into CI logs or issue trackers.
Plan rollback before rollout. Reverting the application code while leaving the new key active can create a mismatch between code and environment. A rollback procedure should specify which app version and which key set go together. Rotation also does not repair a compromised key by itself: revoke access to the old secret, investigate exposure, and rotate all dependent credentials as needed.
Laravel documents the APP_PREVIOUS_KEYS behavior in its encryption guide. Laravel’s 11.0 announcement explains why this capability avoids unnecessary logout during rotation.