Amazon EBS volumes TRIM support
TRIM is an industry-standard command that operating systems use to tell a storage device which blocks no longer hold data, for example after you delete a file. It is a standard space-reclamation mechanism that lets a storage system stop keeping data that the application has already deleted.
Amazon EBS volumes appear to Nitro-based Amazon EC2 instances as Non-Volatile Memory Express (NVMe) block devices. On NVMe devices, the operating system sends TRIM as a deallocate request, part of the standard NVMe command set, so Amazon EBS accepts TRIM through the same interface as any NVMe SSD.
When your operating system issues TRIM for unused blocks, snapshots that you take afterward exclude those blocks. This reduces snapshot size and snapshot storage cost. For more information, see How Amazon EBS snapshots work.
Amazon EBS supports TRIM on all volume types. There is no additional charge for TRIM.
Key considerations
-
TRIM is supported only on volumes attached to Nitro-based Amazon EC2 instances. Xen-based Amazon EC2 instances do not support TRIM.
-
TRIM is not supported in Local Zones, in AWS Wavelength, or on AWS Outposts.
-
TRIM is supported in all commercial AWS Regions and in AWS GovCloud (US).
-
TRIM is not supported in the Middle East (UAE) and Middle East (Bahrain) Regions. TRIM is not supported in the AWS European Sovereign Cloud (Germany) Region, the Amazon Web Services China (Beijing) Region, operated by Sinnet, and the Amazon Web Services China (Ningxia) Region, operated by NWCD.
-
Amazon EBS processes TRIM in 512 KiB blocks. If your application sends TRIM commands that don't align with 512 KiB block boundaries, the partly trimmed blocks count as changed and are captured in your next snapshot.
-
For Multi-Attach enabled volumes, TRIM commands are subject to the same write ordering and I/O fencing requirements as write operations. For more information, see the Multi-Attach considerations and limitations in Attach an EBS volume to multiple EC2 instances using Multi-Attach.
-
TRIM is not supported for Multi-AZ volumes.
-
TRIM is not supported for the following instance type families:
trn2,trn2u, andp6e-gb300.
Volume-level TRIM controls
Amazon EBS tells your Amazon EC2 instance's operating system whether a volume supports TRIM when the volume attaches, as part of the NVMe device capabilities that the volume reports to the guest.
Amazon EBS uses two volume properties to control TRIM and to report whether an attached volume is enabled to accept TRIM commands.
TrimOnAttach-
TrimOnAttachis a settable, volume-level property that specifies whether the volume reports TRIM support to the operating system when the volume is attached to an Amazon EC2 instance. Amazon EBS applies this intent only when the volume attaches to an Amazon EC2 instance, and TRIM support on the volume stays the same for the life of the attachment. You set it when you create or modify a volume. TrimState-
TrimStateis a read-only, per-attachment property that shows whether an attached volume is enabled to accept TRIM commands, based on what was advertised to the guest operating system when the volume attached. It reflects the volume'sTrimOnAttachvalue at the time of attachment, and stays the same for that attachment. An unattached volume has noTrimState. For a Multi-Attach volume, each attachment has its ownTrimState. For more information, see Attach an EBS volume to multiple EC2 instances using Multi-Attach.
TrimOnAttach and TrimState can differ, especially when the
TrimOnAttach property is modified after the volume is attached to an Amazon EC2
instance. Amazon EBS sets the TRIM
advertisement in the NVMe device configuration when the volume attaches, and the operating
system reads it during device discovery. That advertisement is fixed for the life of the
attachment and cannot be changed in place. If you modify TrimOnAttach on a volume
that is already attached, Amazon EBS records the new value and applies it only at the volume's next
attachment.
Enabling TRIM on a volume
To turn on TRIM for a new volume, set TrimOnAttach to enabled
when you create the volume. Amazon EBS applies the TrimOnAttach value when the volume
attaches to a Nitro-based instance, and reports the result in TrimState.
Enable TRIM on an existing, attached volume
For a volume that is already attached, do the following:
-
Use
ModifyVolumeto setTrimOnAttachtoenabled, if it is not already set. -
Detach and reattach the volume. Your
TrimOnAttachvalue takes effect after the volume reattaches.
After the volume reattaches, you can use DescribeVolumes or
DescribeInstances to confirm that TrimState shows as
enabled.
Disabling TRIM on a volume
TRIM is enabled by default on Adaptive Volumes (gp3a and io2a),
unless you override the default.
For more information, see Amazon EBS Adaptive Volumes. TRIM is disabled by default on gp2,
gp3, io1, io2, st1, and sc1
volumes, so a new volume does not have TRIM enabled. If an existing volume has TRIM enabled,
you can disable it as follows:
-
Use
ModifyVolumeto setTrimOnAttachtodisabled. -
Detach and reattach the volume.
You can confirm the result using TrimState in DescribeVolumes
or DescribeInstances.
Beyond the volume level, you can also stop your operating system from issuing TRIM commands. For more information, see Configure your operating system to issue TRIM.
Configure your operating system to issue TRIM
Enabling TRIM using volume-level controls is necessary, but it is not sufficient on its own. Amazon EBS can only act when your operating system sends TRIM commands for blocks that no longer hold data. You must configure your application's operating system for TRIM.
Amazon EBS supports TRIM from most modern operating systems, including Linux and Windows. Some operating system versions are automatically configured to issue TRIM, but make sure that your application is configured to do so.
Operating systems usually issue TRIM in one of two ways:
-
Periodic TRIM: The operating system sends TRIM on a schedule, such as once a week. Examples are the
fstrim.timerservice on Linux and the Optimize Drives schedule on Windows. -
Continuous TRIM: The operating system sends a TRIM command every time a file is deleted, for example with the
discardmount option on Linux.
We recommend periodic TRIM for most workloads. Like write I/Os, TRIM commands use your volume's provisioned IOPS. With continuous TRIM, the file system sends TRIM commands automatically as files are deleted. How often it sends them depends on the operating system and file system. During heavy deletes, such as database compaction or log rotation, these commands can compete with your application, which can reduce available IOPS and increase latency. Running TRIM on a schedule, during a low-activity window and before your regular snapshot, avoids this and still leaves deleted data out of the snapshots you take afterward.
Use the following steps to configure and validate TRIM on your operating system.
Linux
On Linux, the fstrim.timer service runs periodic TRIM. Whether it is
enabled by default depends on your distribution. On Amazon Linux 2023 and Amazon Linux 2027,
the fstrim.timer service is enabled by default, so periodic TRIM runs
automatically once TRIM is enabled on the volume.
To enable or disable periodic TRIM, use the fstrim.timer systemd service.
This service runs space reclamation weekly on mounted file systems.
To enable periodic TRIM, run the following command:
sudo systemctl enable --now fstrim.timer
To disable periodic TRIM, run the following command:
sudo systemctl disable --now fstrim.timer
To verify that the timer is active, run the following command:
systemctl is-enabled fstrim.timer
Continuous TRIM is available through the discard mount option
(mount -o discard), but it generally is not recommended for most
workloads.
Amazon EBS volumes that you create without a snapshot are fully trimmed before they are
attached to your instance. For faster formatting, you can skip the TRIM operation when you
format them. For XFS, use the -K option. For ext4, use the
-E nodiscard option.
sudo mkfs -t xfs -K /dev/xvdf
sudo mkfs -t ext4 -E nodiscard /dev/xvdf
Windows
On Windows Server 2016 and later, TRIM is enabled by default.
To verify that TRIM is enabled, run the following command. A value of 0
means that TRIM is enabled.
fsutil behavior query DisableDeleteNotify
To disable TRIM, run the following command:
fsutil behavior set DisableDeleteNotify 1
To enable TRIM, run the following command:
fsutil behavior set DisableDeleteNotify 0
When you format an Amazon Amazon EBS volume that you created without a snapshot, you can skip TRIM to format faster. You can temporarily disable TRIM during initial formatting, and then re-enable TRIM after formatting is complete, using the preceding commands.
TRIM behavior when you modify a volume
When you modify a volume, the resulting TrimOnAttach setting depends on the
target volume type and on whether you specify a TrimOnAttach value in the
modification request.
When you create a volume or launch an instance, TRIM is enabled by default on Adaptive
Volumes (gp3a and io2a) and disabled by default on all other volume
types (gp2, gp3, io1, io2, st1,
and sc1). You can override either default by specifying TrimOnAttach
in your request.
Adaptive Volumes charge only for the storage that you use, so we recommend that you keep
TRIM enabled on them. When you modify a volume to gp3a or io2a, Amazon EBS
enables TrimOnAttach, even if it was disabled on the source volume. To keep TRIM
disabled, set TrimOnAttach to disabled in the modification request.
When you modify a volume to any other volume type, Amazon EBS keeps the volume's existing
TrimOnAttach setting.
TrimOnAttach is the setting that you request, and TrimState shows
whether the volume is accepting TRIM commands right now. Amazon EBS updates TrimOnAttach
as soon as the modification completes, but TrimState changes only the next time the
volume is attached. If you modify a volume while it's attached, TrimState keeps its
current value until you detach and reattach the volume.
The following table shows the results of common volume modifications. In each row, the
source volume is attached, so TrimState keeps its current value until you detach
and reattach the volume.
| Source volume type | Source TrimOnAttach | Source TrimState | Target volume type | Target TrimOnAttach in the ModifyVolume API call |
Outcome volume type | Outcome TrimOnAttach |
|---|---|---|---|---|---|---|
gp3 |
disabled |
disabled |
gp3a |
Not specified | gp3a |
enabled (uses default for Adaptive Volumes) |
gp3 |
disabled |
disabled |
gp3a |
disabled |
gp3a |
disabled |
gp3 |
disabled |
disabled |
gp3 |
Not specified | gp3 |
disabled |
gp3 |
disabled |
disabled |
gp3 |
enabled |
gp3 |
enabled |
gp3a |
disabled |
disabled |
gp3a |
Not specified | gp3a |
disabled (carried forward explicit intent for Adaptive Volume) |
gp3a |
enabled |
enabled |
gp3a |
disabled |
gp3a |
disabled |
gp3a |
enabled |
enabled |
gp3 |
Not specified | gp3 |
enabled (carried forward) |
gp3a |
disabled |
disabled |
gp3 |
Not specified | gp3 |
disabled (carried forward) |
Best practices
Before you turn on TRIM for a volume, review how TRIM commands affect its performance and test the change with your workload.
TRIM commands count as regular I/O
Amazon EBS handles TRIM commands from your operating system like writes, so they use the volume's provisioned IOPS and throughput. If TRIM runs during peak application load, it might compete with your application's I/O for the same provisioned performance, which can reduce the IOPS available to your application and impact performance.
Check your volume's performance headroom
If your volume regularly runs close to its provisioned IOPS or throughput, turning TRIM on can push it past those limits. Before you turn on TRIM, compare your current usage with the volume's provisioned performance, and consider adding headroom if you need it.
Test with your workload first
How much I/O TRIM generates depends on your operating system, file system, how often you run TRIM, and how much data you delete. Applications or storage layers that manage their own block I/O might also behave differently with TRIM. We recommend that you confirm that your application still meets its performance requirements before you turn it on for all volumes.
Schedule TRIM
For most workloads, run TRIM periodically during a low-activity window, such as weekly, instead of using continuous TRIM. Continuous TRIM sends a command on every deletion, which can cause I/O bursts during heavy deletes such as database compaction or log rotation.
Plan ahead for turning TRIM off
Changing TrimOnAttach takes effect only after you detach and reattach the
volume. If you need to stop TRIM quickly, turn it off in the operating system first, then
change TrimOnAttach at your next maintenance window.
Monitor TRIM
There are three ways to monitor TRIM: your snapshot costs over time, the storage in use on the volume, and the TRIM activity that your operating system sends.
-
Snapshot costs over time — Snapshots that you take after TRIM leave out the blocks that your operating system trimmed. As they replace older snapshots under your retention policy, your total snapshot storage costs may go down. How much depends on how often you delete data and take snapshots. Existing snapshots don't change. For more information, see How Amazon EBS snapshots work.
-
Storage in use on the volume — The
VolumeConsumedBytesAmazon CloudWatch metric shows how much storage on the volume holds data. A drop after a TRIM run means the trimmed blocks are no longer counted as in use. For more information, see Amazon CloudWatch metrics for Amazon EBS. -
TRIM activity — The
trim_opsstatistic in the Amazon EBS detailed performance statistics counts the TRIM commands that the volume receives. Use it to check that your operating system or application is sending TRIM, and to see how much TRIM traffic there is and when. For more information, see Amazon EBS detailed performance statistics.