Created
May 31, 2024 20:40
-
-
Save chrisvasqm/88ea776a33b9d1e877adf000fa6751e6 to your computer and use it in GitHub Desktop.
Delete objects from S3 while excluding some
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const aws = require('aws-sdk'); | |
| // Configure the AWS SDK with your credentials | |
| aws.config.update({ | |
| accessKeyId: 'ACCESS_KEY', | |
| secretAccessKey: 'SECRET_ACCESS_KEY', | |
| region: 'REGION' | |
| }); | |
| // Create an S3 service object | |
| const s3 = new aws.S3(); | |
| // Specify the bucket name and folder prefix | |
| const bucketName = 'BUCKET_NAME'; | |
| const folderPrefix = 'FOLDER_OR_PREFIX'; | |
| // List of user IDs (or folder names) to be excluded | |
| // Replace with actual IDs or folder names | |
| const excludedFolders = ['1', '2', '3', '4']; | |
| // Function to check if a key should be excluded | |
| const shouldExclude = (key) => { | |
| return excludedFolders.some(folder => key.startsWith(`${folderPrefix}${folder}/`)); | |
| }; | |
| // Function to list and delete old objects | |
| const listAndDeleteObjects = (continuationToken) => { | |
| const params = { | |
| Bucket: bucketName, | |
| Prefix: folderPrefix, | |
| ContinuationToken: continuationToken | |
| }; | |
| s3.listObjectsV2(params, (err, data) => { | |
| if (err) { | |
| console.error(err); | |
| return; | |
| } | |
| // Filter objects to exclude certain folders | |
| const objectsToDelete = data.Contents.filter(obj => !shouldExclude(obj.Key)).map(obj => ({ Key: obj.Key })); | |
| if (objectsToDelete.length > 0) { | |
| const deleteParams = { | |
| Bucket: bucketName, | |
| Delete: { | |
| Objects: objectsToDelete | |
| } | |
| }; | |
| s3.deleteObjects(deleteParams, (err, data) => { | |
| if (err) { | |
| console.error(`Error deleting objects: ${err}`); | |
| } else { | |
| data.Deleted.forEach(deleted => console.log(`Deleted object: ${deleted.Key}`)); | |
| } | |
| }); | |
| } | |
| // If there are more objects, continue listing | |
| if (data.IsTruncated) { | |
| listAndDeleteObjects(data.NextContinuationToken); | |
| } | |
| }); | |
| }; | |
| // Start the process | |
| listAndDeleteObjects(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment