# Address cleanse

`address_cleanse` matches a freeform UK address to the closest address on file and scores the match. Use it to clean addresses you already hold, such as a CRM export.

cleanse.rbruby

```ruby
require "ideal_postcodes"

client = IdealPostcodes::Client.new(api_key: "ak_test")
result = client.address_cleanse(query: "9 Malyons Rd, Swanley, BR8 7RE").result

if result.is_a?(IdealPostcodes::Types::GbrCleanseMatch) && result.confidence.positive?
  puts "#{result.match.line_1}, #{result.match.postcode} (#{result.confidence})"
else
  puts "No match"
end
```

## Match or no match

Check the class and the score. A match is a `GbrCleanseMatch` with a `confidence` above 0 and the matched `Address` in `match`. A no-match is a `GbrCleanseNoMatch`, with every score at 0, `match` `nil` and every match level at `NO_MATCH`.

## Separate postcode

Yes. If your records keep the postcode or post town in their own fields, pass them as `postcode:` and `post_town:` alongside `query:` rather than joining them into it. The SDK sends those in the request body and `tags:`, `context:` and `remove_organisation:` in the query string.

## Addresses outside the UK

Pass `context:` with the country's ISO 3166-1 alpha-3 code. Cleanse defaults to the UK. The address datasets your key is licensed for decide which countries it can cleanse.

cleanse\_usa.rbruby

```ruby
result = client.address_cleanse(
  query: "1600 Pennsylvania Avenue NW, Washington, DC 20500",
  context: "usa"
).result
```

The result has the same shape for every country, so a match elsewhere is still a `GbrCleanseMatch` and the check above works unchanged.

## Remove the organisation

Pass `remove_organisation: true`. The organisation and department names are dropped from the address lines and the remaining lines move up. Use it when you deliver to people rather than businesses.

cleanse\_lines.rbruby

```ruby
result = client.address_cleanse(query: "10 Downing Street, London, SW1A 2AA",
                                remove_organisation: true).result

# result.match.line_1 is "10 Downing Street", not the organisation
```

`organisation_name`, `department_name` and the `native` record are unchanged. If removing the names would leave no address lines, they are kept.

## Confidence threshold

It depends on your data. Each wrong, missing or misspelt element lowers the score, and one source tends to repeat the same mistakes. Cleanse a sample, sort it by `confidence` and set your threshold where the matches start to go wrong.

The result also scores each element separately, in `organisation_match`, `premise_match`, `thoroughfare_match`, `locality_match`, `post_town_match` and `postcode_match`. The [API reference](/docs/api/address-cleanse.md) explains each level.

## Cost

One lookup for a match. A no-match costs nothing.

See [`cleanse`](/docs/sdks/ruby/endpoints/address-cleanse.md) for every keyword.
