Sub topic 12
Disabling and deleting keys safely
This chapter assumes you can create a customer managed KMS key, read a key policy, and use the AWS CLI. It also assumes you know that data encrypted under a key becomes unreadable if the key is gone. Deleting a KMS key is the one KMS operation you cannot undo once it completes, so the service gives you two safety stages: disabling, then scheduled deletion.
Why deletion is dangerous
A KMS key never leaves KMS in plaintext. If the key is deleted, every ciphertext produced directly by that key is lost. Every data key that was encrypted under it is lost too. That includes S3 objects, EBS volumes, RDS snapshots, and your own envelope-encrypted files. No AWS support request can bring the key material back.
For that reason, treat deletion as the last step of a process:
- Find out what still uses the key.
- Disable the key and watch for failures.
- Schedule deletion with a waiting period.
- Watch again during the waiting period.
- Let the deletion complete.
Key states
A KMS key is always in one state. The state decides which operations succeed.
| State | Cryptographic operations | Reversible? |
|---|---|---|
| Enabled | Allowed (subject to policy) | Not applicable |
| Disabled | Rejected | Yes, with `enable-key` |
| Pending deletion | Rejected | Yes, with `cancel-key-deletion` until the window ends |
| Deleted | The key no longer exists | No |
Two details matter here. First, schedule-key-deletion works on an enabled key, but the key is not usable while it waits. Second, cancel-key-deletion returns the key to the disabled state, not the enabled state. You must call enable-key afterward to use it again.
Disabling a key
Disabling is a fast, reversible way to test what depends on a key. While a key is disabled, Encrypt, Decrypt, GenerateDataKey, and similar calls fail with a DisabledException. Management calls such as DescribeKey, GetKeyPolicy, and EnableKey still work.
aws kms disable-key --key-id alias/tutorial-key
aws kms describe-key \
--key-id alias/tutorial-key \
--query KeyMetadata.KeyState \
--output text
# Restore the key if something breaks
aws kms enable-key --key-id alias/tutorial-keyThe second command prints Disabled. Run enable-key and the key accepts requests again, with the same key material and the same ARN.
Disabling has a practical limit. Some AWS services cache data keys, so a disabled key may not cause failures right away. An S3 bucket with Bucket Keys, for example, can keep working for a while using a cached bucket-level key. A quiet period after disabling is useful evidence, but it is not proof that nothing depends on the key. Wait longer than your slowest job cycle, such as a monthly batch or a quarterly report, before you move on.
Scheduling deletion
You cannot delete a customer managed key instantly. You schedule deletion and choose a waiting period of 7 to 30 days. The default is 30 days.
aws kms schedule-key-deletion \
--key-id <key-id> \
--pending-window-in-days 30The response contains the DeletionDate. While the key is pending deletion:
- It cannot be used for any cryptographic operation.
- It does not rotate.
- You can still read its metadata and policy.
- You can cancel the deletion.
aws kms cancel-key-deletion --key-id <key-id>
# The key comes back Disabled. Enable it to use it again.
aws kms enable-key --key-id <key-id>Use a short window only for keys you created for tests. Use 30 days for anything that ever protected real data.
Aliases and the key ID behave differently from the key itself. An alias is a separate resource, so it stays in your account after the key is deleted and then points at nothing. Delete the alias on purpose with aws kms delete-alias when you retire the key, so nobody reuses a name that leads nowhere.
Finding what still uses a key
Before you schedule deletion, look for evidence of use. Three sources help:
- CloudTrail. Search for KMS events that reference the key ARN. The
Decryptevents show who is still reading data protected by the key. The previous chapter on auditing explains how to read these events. - Grants. Run
aws kms list-grants --key-id <key-id>. Active grants mean an AWS service or application has delegated access, and they often indicate encrypted resources such as EBS volumes. - Service inventories. Check the services that reference the key, for example S3 bucket default encryption, EBS volumes, RDS instances, and Secrets Manager secrets.
CloudTrail only shows activity inside its retention window. A key that is used once a year may not appear in the last 90 days of event history, so a trail that stores logs in S3 gives better coverage than the console's event history.
Alarming on use of a pending key
The best safety net is a signal that fires when something tries to use a key that is disabled or pending deletion. Such attempts fail, and the failure is logged in CloudTrail with an error code such as DisabledException or KMSInvalidStateException. If you send CloudTrail to CloudWatch Logs, you can build a metric filter on these errors and attach an alarm. A single alarm during the waiting period tells you a dependency exists while you can still cancel.
A common pattern is to alarm on any such error for keys tagged retiring=true:
aws kms tag-resource \
--key-id <key-id> \
--tags TagKey=retiring,TagValue=trueTag the key before you disable it, so your monitoring knows which failures are expected to be investigated.
Controlling who can delete a key
Because deletion is destructive, restrict it. The permissions involved are kms:DisableKey, kms:ScheduleKeyDeletion, and kms:CancelKeyDeletion. A key policy can allow your application role to use the key but give only an administrator role the lifecycle actions:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AdminsManageLifecycle",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/key-admin" },
"Action": [
"kms:DescribeKey",
"kms:DisableKey",
"kms:EnableKey",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
}
]
}This statement gives the administrator role no Decrypt or Encrypt permission. Managing a key and using it are separate duties. For stronger protection, add a policy that denies kms:ScheduleKeyDeletion unless the caller authenticated with multi-factor authentication, using the aws:MultiFactorAuthPresent condition key.
Common mistakes
- Treating disabled as proven unused. Cached data keys and infrequent jobs hide dependencies. Wait through a full business cycle.
- Choosing a 7-day window for a production key. A weekly or monthly process may not run inside that window.
- Forgetting that cancellation leaves the key disabled. Applications keep failing until you call
enable-key. - Deleting a key that protects backups. Old snapshots and backups may still be encrypted under the key even when no live system uses it. Check retention periods first.
- Assuming a replica protects you. A Multi-Region replica key is a separate key resource with its own state, so deleting the primary does not delete replicas, and a replica does not save you if you delete the key you actually used elsewhere. Check the Multi-Region keys chapter for the rules.
- Using deletion to stop an incident. If a key is compromised and you only need to stop access, disable it or edit the key policy. This keeps the data recoverable once you have fixed the problem.
A safe retirement checklist
- Tag the key and set up the error alarm.
- Review CloudTrail and grants for recent use.
- Re-encrypt needed data under a new key.
- Disable the key and wait through a full business cycle.
- Schedule deletion with a 30-day window.
- Watch for alarms until the date arrives.
- Delete the alias after the key is gone.
Exercise
You disable a key and nothing fails for a week, so you schedule deletion with a 7-day window. On day 5 a monthly batch job runs and fails with a KMSInvalidStateException. What can you do, and what must you do afterward to make the job work again?
Show answer
Run aws kms cancel-key-deletion before the window ends, which stops the deletion. The key returns in the Disabled state, so you must then run aws kms enable-key. After that the job can call KMS again and succeed. The lesson is to use a longer window, such as 30 days, and to wait through a full business cycle after disabling.
Exercise
A teammate says that after the waiting period ends you can restore a deleted key from an AWS support request. Is that correct, and what does the alias do after the key is deleted?
Show answer
No. Once the waiting period ends, the key and its key material are deleted permanently and AWS cannot recover them. Data encrypted under the key stays unreadable. The alias remains as a separate resource but points at nothing, so you should delete it yourself with aws kms delete-alias.