Search code examples
pythongoogle-cloud-storagegoogle-cloud-python

GCS delete object using python client doesn't remove the resource using GET uri


Using the GCS python client, deleting a blob doesn't raise any exception. But performing GET on the URI that was just deleted, still returns the resource. The following code is part of a single file Flask app.

from google.cloud import storage as gstorage
storage_client = gstorage.Client()
storage_bucket = storage_client.get_bucket(app.config['STORAGE_BUCKET'])
@bp.route('/verify', methods=['POST'])
def post_verification_photo():
    ...
    crs.execute('SELECT uri FROM photo WHERE id=%s', (photoId,))
    photoUri = crs.fetchone()[0]
    storage_bucket.delete_blob(photoUri[photoUri.rindex('/')+1:])

Note that the object was created with Cache-Control: public, max-age=9999999 header, and also was made public and non-resumable in the metadata.

After execution, there is no error, but GETting the full URI prefixed by the bucket URI still returns the object. Am I confused over the actual use of these methods? Or does the API take some amount of time to remove it from their network? Or, something else?


Solution

  • You don't have to import storage as gstorage. You can follow this Deleting Objects documentation for Python. It has also a link to GitHub with all the functions for managing Blobs.

    I did a little bit of coding myself. You can see the example code on GitHub here. You list all the blobs from the buckets using one function, and you delete one of the blobs using another function. When listing the blobs again, you will see that the blob was deleted successfully. Trying to delete the same blob again will give you an error that the blob wasn't found. Use this example in your code, When you will try to GET the URI, you won't get the object.