github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/docs/bucket/lifecycle/README.md (about)

     1  # Bucket Lifecycle Configuration Quickstart Guide [![Slack](https://slack.min.io/slack?type=svg)](https://slack.min.io) [![Docker Pulls](https://img.shields.io/docker/pulls/minio/minio.svg?maxAge=604800)](https://hub.docker.com/r/minio/minio/)
     2  
     3  Enable object lifecycle configuration on buckets to setup automatic deletion of objects after a specified number of days or a specified date.
     4  
     5  ## 1. Prerequisites
     6  
     7  - Install MinIO - [MinIO Quickstart Guide](https://min.io/docs/minio/linux/index.html#quickstart-for-linux).
     8  - Install `mc` - [mc Quickstart Guide](https://min.io/docs/minio/linux/reference/minio-mc.html#quickstart)
     9  
    10  ## 2. Enable bucket lifecycle configuration
    11  
    12  - Create a bucket lifecycle configuration which expires the objects under the prefix `old/` on `2020-01-01T00:00:00.000Z` date and the objects under `temp/` after 7 days.
    13  - Enable bucket lifecycle configuration using `mc`:
    14  
    15  ```sh
    16  $ mc ilm import play/testbucket <<EOF
    17  {
    18      "Rules": [
    19          {
    20              "Expiration": {
    21                  "Date": "2020-01-01T00:00:00.000Z"
    22              },
    23              "ID": "OldPictures",
    24              "Filter": {
    25                  "Prefix": "old/"
    26              },
    27              "Status": "Enabled"
    28          },
    29          {
    30              "Expiration": {
    31                  "Days": 7
    32              },
    33              "ID": "TempUploads",
    34              "Filter": {
    35                  "Prefix": "temp/"
    36              },
    37              "Status": "Enabled"
    38          }
    39      ]
    40  }
    41  EOF
    42  ```
    43  
    44  ```
    45  Lifecycle configuration imported successfully to `play/testbucket`.
    46  ```
    47  
    48  - List the current settings
    49  
    50  ```
    51  $ mc ilm ls play/testbucket
    52       ID     |  Prefix  |  Enabled   | Expiry |  Date/Days   |  Transition  |    Date/Days     |  Storage-Class   |       Tags
    53  ------------|----------|------------|--------|--------------|--------------|------------------|------------------|------------------
    54  OldPictures |   old/   |    ✓       |  ✓     |  1 Jan 2020  |     ✗        |                  |                  |
    55  ------------|----------|------------|--------|--------------|--------------|------------------|------------------|------------------
    56  TempUploads |  temp/   |    ✓       |  ✓     |   7 day(s)   |     ✗        |                  |                  |
    57  ------------|----------|------------|--------|--------------|--------------|------------------|------------------|------------------
    58  ```
    59  
    60  ## 3. Activate ILM versioning features
    61  
    62  This will only work with a versioned bucket, take a look at [Bucket Versioning Guide](https://min.io/docs/minio/linux/administration/object-management/object-versioning.html) for more understanding.
    63  
    64  ### 3.1 Automatic removal of non current objects versions
    65  
    66  A non-current object version is a version which is not the latest for a given object. It is possible to set up an automatic removal of non-current versions when a version becomes older than a given number of days.
    67  
    68  e.g., To scan objects stored under `user-uploads/` prefix and remove versions older than one year.
    69  
    70  ```
    71  {
    72      "Rules": [
    73          {
    74              "ID": "Removing all old versions",
    75              "Filter": {
    76                  "Prefix": "users-uploads/"
    77              },
    78              "NoncurrentVersionExpiration": {
    79                  "NoncurrentDays": 365
    80              },
    81              "Status": "Enabled"
    82          }
    83      ]
    84  }
    85  ```
    86  
    87  This JSON rule is equivalent to the following MinIO Client command:
    88  ```
    89  mc ilm rule add --noncurrent-expire-days 365 --prefix "user-uploads/" myminio/mydata
    90  ```
    91  
    92  ### 3.2 Automatic removal of noncurrent versions keeping only most recent ones after noncurrent days
    93  
    94  It is possible to configure automatic removal of older noncurrent versions keeping only the most recent `N` using `NewerNoncurrentVersions`.
    95  
    96  e.g, To remove noncurrent versions of all objects keeping the most recent 5 noncurrent versions under the prefix `user-uploads/` 30 days after they become noncurrent ,
    97  
    98  ```
    99  {
   100      "Rules": [
   101          {
   102              "ID": "Keep only most recent 5 noncurrent versions",
   103              "Status": "Enabled",
   104              "Filter": {
   105                  "Prefix": "users-uploads/"
   106              },
   107              "NoncurrentVersionExpiration": {
   108                  "NewerNoncurrentVersions": 5,
   109                  "NoncurrentDays": 30
   110              }
   111          }
   112      ]
   113  }
   114  ```
   115  
   116  This JSON rule is equivalent to the following MinIO Client command:
   117  ```
   118  mc ilm rule add --noncurrent-expire-days 30 --noncurrent-expire-newer 5 myminio/mydata
   119  ```
   120  
   121  #### 3.2.a Automatic removal of noncurrent versions keeping only most recent ones immediately (MinIO only extension)
   122  
   123  This is available only on MinIO as an extension to the NewerNoncurrentVersions feature. The following rule makes it possible to remove older noncurrent versions
   124  of objects under the prefix `user-uploads/` as soon as there are more than `N` noncurrent versions of an object.
   125  
   126  ```
   127  {
   128      "Rules": [
   129          {
   130              "ID": "Keep only most recent 5 noncurrent versions",
   131              "Status": "Enabled",
   132              "Filter": {
   133                  "Prefix": "users-uploads/"
   134              },
   135              "NoncurrentVersionExpiration": {
   136                  "NewerNoncurrentVersions": 5
   137              }
   138          }
   139      ]
   140  }
   141  ```
   142  Note: This rule has an implicit zero NoncurrentDays, which makes the expiry of those 'extra' noncurrent versions immediate.
   143  
   144  #### 3.2.b Automatic removal of all versions (MinIO only extension)
   145  
   146  This is available only on MinIO as an extension to the Expiration feature. The following rule makes it possible to remove all versions of an object under 
   147  the prefix `user-uploads/` as soon as the latest object satisfies the expiration criteria. 
   148  
   149  > NOTE: If the latest object is a delete marker then filtering based on `Filter.Tags` is ignored and 
   150  > if the DELETE marker modTime satisfies the `Expiration.Days` then all versions of the object are 
   151  > immediately purged.
   152  
   153  ```
   154  {
   155      "Rules": [
   156          {
   157              "ID": "Purge all versions of an expired object",
   158              "Status": "Enabled",
   159              "Filter": {
   160                  "Prefix": "users-uploads/"
   161              },
   162              "Expiration": {
   163                  "Days": 7,
   164                  "ExpiredObjectAllVersions": true
   165              }
   166          }
   167      ]
   168  }
   169  ```
   170  
   171  ### 3.3 Automatic removal of delete markers with no other versions
   172  
   173  When an object has only one version as a delete marker, the latter can be automatically removed after a certain number of days using the following configuration:
   174  
   175  ```
   176  {
   177      "Rules": [
   178          {
   179              "ID": "Removing all delete markers",
   180              "Expiration": {
   181                  "DeleteMarker": true
   182              },
   183              "Status": "Enabled"
   184          }
   185      ]
   186  }
   187  ```
   188  
   189  ## 4. Enable ILM transition feature
   190  
   191  In Erasure mode, MinIO supports tiering to public cloud providers such as GCS, AWS and Azure as well as to other MinIO clusters via the ILM transition feature. This will allow transitioning of older objects to a different cluster or the public cloud by setting up transition rules in the bucket lifecycle configuration. This feature enables applications to optimize storage costs by moving less frequently accessed data to a cheaper storage without compromising accessibility of data.
   192  
   193  To transition objects in a bucket to a destination bucket on a different cluster, applications need to specify a transition tier defined on MinIO instead of storage class while setting up the ILM lifecycle rule.
   194  
   195  > To create a transition tier for transitioning objects to a prefix `testprefix` in `azurebucket` on Azure blob using `mc`:
   196  
   197  ```
   198   mc admin tier add azure source AZURETIER --endpoint https://blob.core.windows.net --access-key AZURE_ACCOUNT_NAME --secret-key AZURE_ACCOUNT_KEY  --bucket azurebucket --prefix testprefix1/
   199  ```
   200  
   201  > The admin user running this command needs the "admin:SetTier" and "admin:ListTier" permissions if not running as root.
   202  
   203  Using above tier, set up a lifecycle rule with transition:
   204  
   205  ```
   206   mc ilm add --expiry-days 365 --transition-days 45 --storage-class "AZURETIER" myminio/srcbucket
   207  ```
   208  
   209  Note: In the case of S3, it is possible to create a tier from MinIO running in EC2 to S3 using AWS role attached to EC2 as credentials instead of accesskey/secretkey:
   210  
   211  ```
   212  mc admin tier add s3 source S3TIER --bucket s3bucket --prefix testprefix/ --use-aws-role
   213  ```
   214  
   215  Once transitioned, GET or HEAD on the object will stream the content from the transitioned tier. In the event that the object needs to be restored temporarily to the local cluster, the AWS [RestoreObject API](https://docs.aws.amazon.com/AmazonS3/latest/API/API_RestoreObject.html) can be utilized.
   216  
   217  ```
   218  aws s3api restore-object --bucket srcbucket \
   219  --key object \
   220  --restore-request Days=3
   221  ```
   222  
   223  ### 4.1 Monitoring transition events
   224  
   225  `s3:ObjectTransition:Complete` and `s3:ObjectTransition:Failed` events can be used to monitor transition events between the source cluster and transition tier. To watch lifecycle events, you can enable bucket notification on the source bucket with `mc event add`  and specify `--event ilm` flag.
   226  
   227  Note that transition event notification is a MinIO extension.
   228  
   229  ## Explore Further
   230  
   231  - [MinIO | Golang Client API Reference](https://min.io/docs/minio/linux/developers/go/API.html)
   232  - [Object Lifecycle Management](https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html)