user3553054
user3553054

Reputation:

How to delete old versions automatically from google-cloud-storage

I am writing files html files to GCS on every update to site related data. The website maintains versions so that manual revert is an option. I need to delete files of previous (not last) versions that are older than 2 days. Is there a way in GCS configurations where I can set expiry on previous versions while creating the next version? or a scheduled delete based creation date of next version?

If not, what is the best way to do it from app-engine cron?

Upvotes: 4

Views: 4331

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41099

You can enable object versioning in your bucket, and then use Lifecycle configuration to set up the rules for old versions. For example, to delete versions that have not been current for more than 30 days:

echo '
{
  "rule":
  [
    {
      "action": {"type": "Delete"},
      "condition": {
        "daysSinceNoncurrentTime": 30
      }
    }
  ]
}
' > /tmp/lifecycle.json
gsutil lifecycle set /tmp/lifecycle.json gs://yourbucket

Alternatively, you can remember keys for old versions in the Datastore. You can periodically query for these objects, and use GSC keys or file names to delete the old files.

Upvotes: 6

Related Questions