Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hassenius/10248966 to your computer and use it in GitHub Desktop.
Save hassenius/10248966 to your computer and use it in GitHub Desktop.
bulkdeleteAllSwiftObjectsAndContainers.sh
#!/bin/bash
################################################################################
# ©Copyright IBM Corporation 2014.
#
# LICENSE: MIT (http://opensource.org/licenses/MIT)
################################################################################
apiKey="<pasteKeyHere>"
userName="<pastUsernameHere>"
endPoint="https://ams01.objectstorage.softlayer.net/auth/v1.0/"
echo "Getting auth information"
curl -i -H "X-Auth-Key: ${apiKey}" -H "X-Auth-User: ${userName}" ${endPoint} > authInfo
echo "Extracting AuthToken"
authToken=$(grep X-Auth-Token: authInfo | cut -c15- | tr -d '\r' )
echo "Extracting URL"
authUrl=$(grep X-Storage-Url: authInfo | cut -c16- | tr -d '\r' )
# Get Containers and place them in a containers file
echo "Getting containers"
curl -X GET -H "X-Auth-Token: ${authToken}" ${authUrl} > containers
# Get number of containers
conCount=$(wc -l < containers)
echo "Cycling through containers"
for container in $( cat containers ); do
# While there is still content in the container, extract content
while [ 1 ]; do
echo "Extracting objects in container ${container}"
curl -X GET -H "X-Auth-Token: ${authToken}" ${authUrl}/${container} > objects
# Stop if container is empty
if [[ $( wc -l < objects) -lt 1 ]]; then
echo "Container is now empty"
break
fi
# Cycle through objects and delete each in turn
objCount=0
reportInterval=1000
maxThreads=20
echo "Starting to delete objects. Will report every ${reportInterval} objects per round (max 10 000 objects per round)"
for object in $( cat objects ); do
# Fill the thread count, then wait 3 seconds
while [[ $(ps aux | grep curl | wc -l) -gt $maxThreads ]]; do
sleep 3
done
curl -X DELETE -H "X-Auth-Token: ${authToken}" ${authUrl}/${container}/${object} &
let objCount=objCount+1
# Let users know where we are at regular intervals
if [[ $((objCount%${reportInterval})) == 0 ]]; then
echo "Deleted ${objCount} objects this round"
fi
done
done
# We probably need to wait for all the threads to complete to delete properly
while [[ $(ps aux | grep curl | wc -l) -gt 1 ]]; do
sleep 3
done
# If we got to this point container should be empty
echo "Deleting container ${container} ( ${authUrl}/${container} )"
curl -X DELETE -H "X-Auth-Token: ${authToken}" ${authUrl}/${container}
done
echo "All done, all containers for ${userName} in ${endPoint} should now be removed...."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment