Auto Delete SQS queue
At the moment, AWS SQS does not provide a mechanism to automatically delete a queue when it is empty for a certain number of days. Even I feel like this is a needed feature. But there are ways to tackle this problem.
Mentioned below, are a few ways to delete the AWS SQS according to the scenario in your question. You can select which suits you the most.
- Maintain a small Database Table which keeps the mapping between the Server IP and Queue URL. You can insert values into this table, when a server starts itself. Maintain a Cloudwatch Rule that will invoke a Lambda, which will go through the values in the table to see if the server is running or not (probably by a heartbeat). If a particular server is not running, simply get the related SQS URL and delete that specific Queue. (I have suggested Lambda here because it is cheap)
sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));
Whenever a Server is started, it can send an Email to a person with the Server IP and SQS URL using SNS. Using a CloudWatch Rule, invoke a Lambda from time to time, and get all Instances, and check if any instance is down. If an instance is down, send an email to the relevant person using SNS, emailing that this server is down. It is semi-automatic, where the use can manually delete the queue after seeing the email.
Simply let the Empty queues be on its own. There is no limit as to how many queues can be made inside AWS. So why bother to delete them if that process is hard. Simply create new Queue as you go along. (See: No Max number of SQS Queue Limitation)
There is no method to auto-delete queues. You could use tags to mark resources connected (i.e. tag queues or other resources with their respective instance-id on creation), and use a simple script that would read the said tag and delete if that instance-id does not exist.
here is how to do it on cli: https://docs.aws.amazon.com/cli/latest/reference/sqs/tag-queue.html
(I'm assuming that by server you mean EC2 instance. Ip could also be used)