> For the complete documentation index, see [llms.txt](https://docs-epc.gitbook.io/ncs-documents/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-epc.gitbook.io/ncs-documents/storage/object-storage-s3/delete-lifecycle-policies.md).

# Delete Lifecycle Policies

หากผู้ใช้งานต้องการสร้าง lifecycle policy เพื่อที่จะสามารถลบ object ออกไปได้โดยอัตโนมัติ เมื่อเวลาผ่านไปตามช่วงเวลาที่กำหนด

## Prerequisite

* Bucket ที่ต้องการใช้งาน policy ([Create an Object Storage Bucket](/ncs-documents/storage/object-storage-s3/create-an-object-storage-bucket.md))
* Sub-User ที่มีสิทธิ์การเข้าถึง Bucket ([Create Object Storage Sub-User](/ncs-documents/storage/object-storage-s3/create-an-object-storage-sub-user.md))
* s3cmd ([Access S3 Bucket with s3cmd](/ncs-documents/storage/object-storage-s3/access-s3-bucket-with-s3cmd.md))

## Instructions

1. ทำการสร้างไฟล์ xml เพื่อทำการ config policy โดยมี block ที่สามารถใช้งานได้ดังนี้
   1. ID -> ใช้กำหนดชื่อของ policy
   2. Filter -> ใช้ในการกรอง object ที่ต้องการใช้งาน policy
   3. Prefix -> ใช้ในการเลือกเฉพาะ object ที่มีคำนำหน้าที่ตรงกันให้ใช้งาน policy, หากไม่ต้องการทำการกรอง object ให้เว้นว่างไว้
   4. Status -> ใช้ในเปิดหรือปิดใช้งานของ policy
   5. Expiration -> ใช้กำหนดระยะเวลาของ object ที่ต้องการจะใช้งาน policy

{% code title="ตัวอย่าง policy ที่จะทำการลบ object ที่อยู่ใน folder logs และมีอายุมากกว่า 30 วัน" %}

```xml
<LifecycleConfiguration>
    <Rule>
        <ID>delete-logs</ID>
        <Filter>
        <Prefix>logs/</Prefix>
        </Filter>
        <Status>Enabled</Status>
        <Expiration>
            <Days>30</Days>
        </Expiration>
    </Rule>
</LifecycleConfiguration>
```

{% endcode %}

การใช้งานหลาย rules สามารถนำ rules ที่ต้องการกำหนดเพิ่มมาต่อกันได้ดังตัวอย่างนี้

<pre class="language-xml" data-title="เพิ่ม rule ที่จะทำการลบ object ที่อยู่ใน folder backup และมีอายุมากกว่า 3 วัน"><code class="lang-xml"><strong>&#x3C;LifecycleConfiguration>
</strong>    &#x3C;Rule>
        &#x3C;ID>delete-logs&#x3C;/ID>
        &#x3C;Filter>
        &#x3C;Prefix>logs/&#x3C;/Prefix>
        &#x3C;/Filter>
        &#x3C;Status>Enabled&#x3C;/Status>
        &#x3C;Expiration>
            &#x3C;Days>30&#x3C;/Days>
        &#x3C;/Expiration>
    &#x3C;/Rule>
    &#x3C;Rule>
        &#x3C;ID>delete-backup&#x3C;/ID>
        &#x3C;Filter>
        &#x3C;Prefix>backup/&#x3C;/Prefix>
        &#x3C;/Filter>
        &#x3C;Status>Enabled&#x3C;/Status>
        &#x3C;Expiration>
            &#x3C;Days>3&#x3C;/Days>
        &#x3C;/Expiration>
    &#x3C;/Rule>
&#x3C;/LifecycleConfiguration>
</code></pre>

2. สามารถนำ policy ที่สร้างมาใช้งานได้ด้วยคำสั่ง s3cmd setlifecycle <ไฟล์ policy> \<bucket เป้าหมาย>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FXGz15C3i0pxXtnbiMi9I%2FScreenshot%202567-02-08%20at%2017.50.22.png?alt=media&amp;token=2ff4488a-fe17-4cf9-8587-97e9f9a3be33" alt=""><figcaption></figcaption></figure>

3. สามารถเรียกดู policy ได้ด้วยคำสั่ง s3cmd getlifecycle \<bucket เป้าหมาย>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FOjr48cMFfJGU2ZPultVX%2FScreenshot%202567-02-08%20at%2017.50.29.png?alt=media&amp;token=8d02e49b-af1e-46a7-a9a4-c66a6ed88702" alt=""><figcaption></figcaption></figure>

4. สามารถลบ policy ได้ด้วยคำสั่ง s3cmd dellifecycle \<bucket เป้าหมาย>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FHncTWVjNFb9GvSGL40eN%2FScreenshot%202567-02-08%20at%2017.54.11.png?alt=media&amp;token=99d0bf8a-89a9-4439-8d64-edb1c87b0c98" alt=""><figcaption></figcaption></figure>

***

อ้างอิง

<https://docs.aws.amazon.com/AmazonS3/latest/userguide/lifecycle-configuration-examples.html>
