Recommand · June 5, 2021 0

How to delete S3 object using cronjob

I have a python file in my EC2 which connects to S3 and deletes all versions of objects.

My S3delete.py

session = boto3.Session()
s3 = session.resource(service_name='s3')
bucket = s3.Bucket('mys3bucket-delete-me')
bucket.object_versions.delete()

In my job.sh I call this python file

#!/bin/bash
python s3delete.py

Make my file executable chmod +x job.sh and execute ./job.sh

This works fine and deletes all objects of my s3 bucket.

Issue is when I call this job.sh within crontab sudo crontab -e every minutes and it doesn’t execute
*/1 * * * * /home/ec2-user/job.sh

Please guide me what’s wrong?