A trim command (known as TRIM in the ATA command set, and UNMAP in the SCSI command set) allows an operating system to inform a storage medium which blocks of data are no longer considered to be "in use" and therefore can be erased internally. TRIM is primarily used on solid-state drives (SSDs), but is also used on some shingled magnetic recording (SMR) hard drives.
Trim commands were introduced soon after SSDs. Because SSDs use different technology from earlier magnetic hard drives, the preexisting techniques for storing data caused early SSDs to become slower as they were used more. With trimming, the SSD can efficiently clear unneeded data that would otherwise slow future write operations.Shimpi, Anand Lal. (18 March 2009). p. 10. TRIM is an ATA command (Advanced Technology Attachment Command) that allows an OS to inform SSD about the blocks of data no longer in use. The SSD then deletes such blocks of data to make a way for newer blocks of data. TRIM is basically used for enhancing the performance and life span of the SSD. Unfortunately, the boon of TRIM becomes a bane for data recovery from SSD. Therefore, it is strongly recommended to regularly make a backup copy of your data using a reliable tool (for example Disk Drill for Windows), especially if your OS is warning about SSD failing.
Although tools to "reset" some drives to a fresh state were already available before the introduction of trimming, they also delete all data on the drive, which makes them impractical to use for ongoing optimization.Shimpi, Anand Lal. (18 March 2009). p. 11. As of 2010, many SSDs and many flash removable storage devices had internal garbage collection mechanisms for certain filesystem(s) (such as FAT and NTFS) that worked independently of trimming. Although this successfully maintained their lifetime even under operating systems that did not support trim, it had the associated drawbacks of increased write amplification and wear of the flash cells, as well as decreased write performance of flash cells.
Background Many file systems handle delete operations by flagging data blocks as "not in use".Shimpi, Anand Lal. (18 March 2009). p. 7. As a result, storage media devices generally do not know which sectors/pages are truly in use and which can be considered free space. Contrary to (for example) an overwrite operation, a delete will not involve writing to the sectors that contained the data. Since a common SSD has no knowledge of the file system structures, including the list of unused blocks/sectors, the storage medium remains unaware that the blocks have become available. This allows undelete tools to recover (parts of) files from electromechanical hard disks, despite the files being reported as "deleted" by the operating system. But it also means that when the operating system later performs a write operation to one of the sectors, which it considers free space, it effectively becomes an overwrite operation from the point of view of the storage medium. For magnetic disks, an overwrite of existing data is no different from writing into an empty sector, but because of how some SSDs function at the lowest level, an overwrite produces significant overhead compared to writing data into an empty page, potentially crippling write performance.
SSDs store data in flash memory cells that are grouped into pages typically of 4 to 16 KiB, grouped together into blocks of typically 128 to 512 pages, for example 512 kiB blocks that group 128 pages of 4 kiB each. NAND flash memory cells can be directly written to only when they are empty. If they happen to contain data, the contents must be erased before a write operation. An SSD write operation can be done to a single page but, due to hardware limitations, erase commands always affect entire blocks.Shimpi, Anand Lal. (18 March 2009). p. 5. Consequently, writing data to empty pages on an SSD is very fast, but slows down considerably once previously written pages need to be overwritten. Since an erase of the cells in the page is needed before it can be written to again, but only entire blocks can be erased, an overwrite will initiate a read-erase-modify-write cycle:Shimpi, Anand Lal. (18 March 2009). p. 8. the contents of the entire block are stored in cache, then the entire block is erased from the SSD, then the overwritten page(s) is written into the cached block, and only then can the entire updated block be written to the flash medium. This phenomenon is known as write amplification.
Operation The TRIM command enables an operating system to notify the drive of pages which no longer contain valid data. For a file deletion operation, the operating system will mark the file's sectors as free for new data, then send a TRIM command to the drive. After trimming, the drive will not preserve any contents of the block when writing new data to a page of flash memory, resulting in less write amplification (fewer writes), higher write throughput (no need for a read-erase-modify sequence), thus increasing drive life.
Different drives implement the command somewhat differently, so performance can vary.
TRIM tells the drive to mark an LBA region as invalid and subsequent reads on the region will not return any meaningful data. For a very brief time, the data could still reside on the flash internally. After the TRIM command is issued and garbage collection has taken place, recovery of deleted or corrupted data can be difficult or impossible, depending on the drive's firmware implementation of the command.
Implementation Operating system support The TRIM command is beneficial only if the drive implements it and the operating system requests it. The table below identifies each notable operating system and the first version supporting the command. Additionally, older solid-state drives designed before the addition of the TRIM command to the ATA standard will need firmware updates, otherwise the new command will be ignored. However, not every drive can be upgraded to support trimming.
The support for TRIM also varies by what the particular filesystem driver on the operating system is capable of, since only a program with an understanding of what parts of the disk are free space can safely issue the command, and on the system level this ability tends to lie in the filesystem driver itself.
Operating SystemSupported since Notes DragonFly BSD 2011-05May 2011 FreeBSD 2010-078.1 – July 2010 Support was added at the block device layer in 8.1. Filesystem support was added in FreeBSD 8.3 and FreeBSD 9, beginning with UFS. ZFS trimming support was added in FreeBSD 9.2. FreeBSD 10 supports trimming on software RAID configurations. NetBSD 2012-10October 2012 Linux 2008-12-252.6.28–25 December 2008 Initial support for discard operations was added for FTL NAND flash devices in 2.6.28. Support for the ATA TRIM command was added in 2.6.33.
Not all filesystems make use of trim. Among the filesystems that can issue trim requests automatically are ext4, Btrfs, FAT, GFS2, JFS, XFS, ZFS, and NTFS-3G. However, in some distributions, this is disabled by default due to performance concerns, in favor of scheduled trimming on supported SSDs. Ext3, NILFS2 and OCFS2 offer ioctls to perform offline trimming. The TRIM specification calls for supporting a list of trim ranges, but as of kernel 3.0 trim is only invoked with a single range that is slower.
In many newer Linux distributions, systemd provides fstrim.timer unit. TRIM for swap partition can be enabled by swapon utility or fstab. macOS 2011-06-2310.6.8–23 June 2011 Although the AHCI block device driver gained the ability to display whether a device supports the TRIM operation in 10.6.6 (10J3210), the functionality itself remained inaccessible until 10.6.8, when the TRIM operation was exposed via the IOStorageFamily and filesystem (HFS+) support was added. Until 10.10.4, Mac OS X natively enabled TRIM only for Apple-branded SSDs, except for external removable SSDs; third-party utilities are available to enable it for other brands. Old third party TRIM drivers stopped working as of the Yosemite update. Updated drivers now exist that work with OS X Yosemite. In Mac OS X update 10.10.4, Apple added a command line utility, trimforce, that can be used to enable TRIM on third-party SSDs. Microsoft Windows 2009–10Windows 7 and Windows Server 2008 R2 – October 2009 Alt URLWindows 7 initially supported TRIM only for drives in the AT Attachment family including Parallel ATA and Serial ATA, and did not support this command for any other devices including Storport PCI-Express SSDs even if the device itself would accept the command.Geoff Gasior (2012) OCZ's RevoDrive 3 X2 240GB solid-state drive It is confirmed that with native Microsoft drivers the TRIM command works on Windows 7 in AHCI and legacy IDE / ATA Mode. Windows 8 and later Windows operating systems support the unmap command for devices that use the SCSI driver stack, including USB Attached SCSI Protocol (UASP). Windows 8.1 and later Windows operating systems support the TRIM command for NVM Express SSDs. Microsoft has released an update for Windows 7 that adds NVM Express support including TRIM for PCIe SSDs.
TRIM is known to be supported for ReFS and NTFS, both of which implement a DisableDeleteNotify switch for disabling it. It is also supported for Bitlocker volumes with ReFS and NTFS volumes within. However, Windows does not support TRIM on exFAT or the older FAT/FAT32 filesystems. OpenSolaris 2010-07July 2010 Android 2013-74.3 – 24 July 2013"Android 4.3 announced, rolling out to Nexus devices today".The Verge. 24 July 2013. Retrieved 24 July 2013.Runs fstrim automatically up to once every 24 hours if the device has been idle for at least an hour and is at least 80% charged (30% if connected to a charger). Actual implementations may differ from OEMs. Windows Phone Windows Phone 7 Supports TRIM since Windows Phone 7. iOS iOS 4 Supports TRIM since iOS 4 and iPhone 4.
RAID issues , support for the TRIM command is not implemented in most hardware-based RAID technologies. However, software RAID implementations often do include support for TRIM.
Windows Windows 10 offers support for TRIM in SSD ID volumes using the "optimize drives" option when configuring a RAID volume.
macOS The macOS RAID driver does not support TRIM. This is true for all versions of Mac OS X from 10.7 through macOS 10.12.
Linux TRIM is available with RAID volumes in post-January-2011 releases of the Linux kernel's dmraid, which implements BIOS-assisted "fake hardware RAID" support, and which now passes through any TRIM requests from the filesystem that sits on the RAID array.
Not to be confused with dmraid, Linux's general-purpose software RAID system, mdraid, has experimental support for batch-based (rather than live, upon file deletion) TRIM on RAID 1 arrays when systems are configured to periodically run the mdtrim utility on filesystems (even those like ext3 without native TRIM support). In later versions of Linux, e.g. Red Hat Enterprise Linux 6.5 and beyond, mdraid supports actually passing through TRIM commands in real-time, rather than just as a batch job.
However, Red Hat recommends against using software RAID levels 1, 4, 5, and 6 on SSDs with most RAID technologies, because during initialization, most RAID management utilities (e.g. Linux's mdadm) write to all blocks on the devices to ensure that checksums (or drive-to-drive verifies, in the case of RAID 1 and 10) operate properly, causing the SSD to believe that all blocks other than in the spare area are in use, significantly degrading performance.
On the other hand, Red Hat does recommend the use of RAID 1 or RAID 10 for LVM RAIDs on SSDs, as these levels support TRIM ("discard" in Linux terminology), and the LVM utilities do not write to all blocks when creating a RAID 1 or RAID 10 volume.
Firmware-based RAID