Clean Up AWS Resources - Amazon Workmail
Tags: aws, workmail, hosting, cloud
Published: 2023-03-25 14:23:00

So far, we have done many things to explore how Amazon Workmail works so we can host our email services there. In this section, I'll show you how to delete any resources we created within Amazon Workmail through AWS CLI. Please don't misunderstand! I'm not here to ask you to stop using AWS services but I'm here to show you how to delete it in case you may want to create a new resource or replace an existing one.

If you have followed me on the second post of this series, here's the cleanup process. It has its own flow to delete the resources, so please follow the following instructions without missing a single step.

## Deregister User & Group From Workmail

It will disassociate a user/group, mark it as no longer used and change the state to disabled. Here we will create a simple bash script for the repetitive tasks.

Before that, let's gather all the information needed (ID of user/group to be deleted)!

```bash
$ aws workmail list-groups --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 | grep Id
  Id: bcefb7d0-1f5a-45e4-8ef4-853a74823e86
$ aws workmail list-users --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 | grep Id
  Id: 3815a14e-e0d1-4d31-b998-bb290589191c
  Id: 4b1d1dd0-4c9a-451a-83de-4145063999f0
  Id: 510f7b96-800d-47e2-a869-c3c47af4e9ea
  Id: a036c622-4d14-4075-b2a4-9a17975cbc83
```

Then, we will copy-paste the ID:

```bash
$ cat delete-emails.sh 
#! /bin/sh
aws workmail deregister-from-work-mail --entity-id bcefb7d0-1f5a-45e4-8ef4-853a74823e86 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 
aws workmail deregister-from-work-mail --entity-id 3815a14e-e0d1-4d31-b998-bb290589191c --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail deregister-from-work-mail --entity-id 4b1d1dd0-4c9a-451a-83de-4145063999f0 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail deregister-from-work-mail --entity-id 510f7b96-800d-47e2-a869-c3c47af4e9ea --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail deregister-from-work-mail --entity-id a036c622-4d14-4075-b2a4-9a17975cbc83 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
$ chmod +x delete-emails.sh 
$ ./delete-emails.sh
```

Now all users and groups are disabled and ready to be deleted:

```bash
$ aws workmail list-groups --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 | grep State
  State: DISABLED
$ aws workmail list-users --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 | grep State
  State: DISABLED
  State: DISABLED
  State: DISABLED
  State: DISABLED
```

## Delete User & Group

```bash
$ cat delete-usergroup.sh 
#! /bin/sh
aws workmail delete-group --group-id bcefb7d0-1f5a-45e4-8ef4-853a74823e86 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail delete-user --user-id 3815a14e-e0d1-4d31-b998-bb290589191c --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail delete-user --user-id 4b1d1dd0-4c9a-451a-83de-4145063999f0 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail delete-user --user-id 510f7b96-800d-47e2-a869-c3c47af4e9ea --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
aws workmail delete-user --user-id a036c622-4d14-4075-b2a4-9a17975cbc83 --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
$ chmod +x delete-usergroup.sh 
$ ./delete-usergroup.sh
```

Check the state one more time!

```bash
$ aws workmail list-groups --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1
Groups:
- DisabledDate: '2023-03-23T21:06:45.687000+07:00'
  EnabledDate: '2023-03-23T20:40:45.076000+07:00'
  Id: bcefb7d0-1f5a-45e4-8ef4-853a74823e86
  Name: developers
  State: DELETED
$ aws workmail list-users --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --region us-east-1 | grep State
  State: DELETED
  State: DELETED
  State: DELETED
  State: DELETED
```

## Deregister External Domain

If we use an external domain as the default one, please change it first at least to the domain alias provided by Workmail.

```bash
$ aws workmail update-default-mail-domain --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --domain-name dhona.awsapps.com --region us-east-1
$ aws workmail deregister-mail-domain --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --domain-name dhona.xyz --region us-east-1
```

## Delete The Organization

This is the last step and make sure you have deleted all things above. If you find an error, please repeat those tasks because you may skip some steps.

```bash
$ aws workmail delete-organization --organization-id m-fb75a642ab0f4745b33b54f729f6af01 --delete-directory --region us-east-1
OrganizationId: m-fb75a642ab0f4745b33b54f729f6af01
State: Deleting
```

Alright! That's it for now! I think we can do more things with Amazon Workmail so I won't say that this is the last part of this series. Please look forward to it and don't forget to follow this blog! Thank you!