Manage lifecycle rules

Last Updated: Jun 27, 2017

OSS allows you to set lifecycle rules for buckets to automatically remove expired objects and save the storage space. You can set multiple lifecycle rules. A lifecycle rule contains the following parts:

  • Rule ID: The ID is used for identifying the rule and cannot be repeated.
  • Prefix of objects subject to the rule: The rule is only applicable to the objects with the specified prefix.
  • Expiration time: It can be specified in either of the following ways:
    • Specify after how many days since the last modification will the object be deleted.
    • Specify that the object will expire on a specific day, that is, all objects with the specified prefix will expire after the day, regardless of the last modification time of the objects. This method is not recommended.
  • Take effect or not

For more information about lifecycle, refer to Object Lifecycle.

Set lifecycle rules

Use putBucketLifecycle to set a lifecycle rule:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>'
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: '<Your bucket name>'
  8. });
  9. co(function* () {
  10. var result = yield client.putBucketLifecycle('bucket-name', 'region', [
  11. {
  12. id: 'rule1',
  13. status: 'Enable',
  14. prefix: 'foo/',
  15. days: 3
  16. },
  17. {
  18. id: 'rule2',
  19. status: 'Disabled',
  20. date: '2022-10-11T00:00:00.000Z'
  21. }
  22. ]);
  23. console.log(result);
  24. }).catch(function (err) {
  25. console.log(err);
  26. });

View lifecycle rules

Use getBucketLifecycle to view lifecycle rules:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>'
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: '<Your bucket name>'
  8. });
  9. co(function* () {
  10. var result = yield client.getBucketLifecycle('bucket-name', 'region');
  11. console.log(result);
  12. }).catch(function (err) {
  13. console.log(err);
  14. });

Clear lifecycle rules

Use deleteBucketLifecycle to set a null Rule array to clear the lifecycle rules:

  1. var co = require('co');
  2. var OSS = require('ali-oss')
  3. var client = new OSS({
  4. region: '<Your region>'
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: '<Your bucket name>'
  8. });
  9. co(function* () {
  10. var result = yield client.deleteBucketLifecycle('bucket-name', 'region');
  11. console.log(result);
  12. }).catch(function (err) {
  13. console.log(err);
  14. });
Thank you! We've received your feedback.