Cancel a Number

If you no longer require a Nexmo virtual number that you have purchased you can cancel it.

This page shows you how to cancel a number programmatically.

You can also cancel a number online, using the developer dashboard or from the command line, using the Nexmo CLI.

Replace the following variables in the sample code with your own values:

Name Description
NEXMO_API_KEY Your Nexmo API key
NEXMO_API_SECRET Your Nexmo API secret
COUNTRY_CODE The two digit country code for the number you want to cancel. For example: GB for the United Kingdom.
NEXMO_NUMBER The Nexmo virtual number you want to cancel. Omit the leading zero but include the international dialing code. For example: 447700900000.

Write the code

Add the following to cancel-number.sh:

Copy to Clipboard
curl -X POST \
  "https://rest.nexmo.com/number/cancel?api_key=$NEXMO_API_KEY&api_secret=$NEXMO_API_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "country=$COUNTRY_CODE&msisdn=$NEXMO_NUMBER"

View full source

Run your code

Save this file to your machine and run it:

sh cancel-number.sh

Prerequisites

Install dependencies

npm install nexmo
Initialize your dependencies

Create a file named cancel.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo')

const nexmo = new Nexmo(
  {
    apiKey: NEXMO_API_KEY,
    apiSecret: NEXMO_API_SECRET
  },
  {
    debug: true
  }
)

View full source

Write the code

Add the following to cancel.js:

Copy to Clipboard
nexmo.number.cancel(COUNTRY_CODE, NEXMO_NUMBER, (err, res) => {
  if(err) {
    console.error(err)
  }
  else {
    console.log(JSON.stringify(res, null, 2))
  }
})

View full source

Run your code

Save this file to your machine and run it:

node cancel.js

Prerequisites

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named cancel.php and add the following code:

Copy to Clipboard
$basic = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client($basic);

View full source

Write the code

Add the following to cancel.php:

Copy to Clipboard
try {
  $client->numbers()->cancel(NEXMO_NUMBER);
  echo "Number cancelled";
}

catch(Exception $e) {
  echo "Error cancelling number";
}

View full source

Run your code

Save this file to your machine and run it:

php cancel.php

Prerequisites

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named cancel.py and add the following code:

Copy to Clipboard
import nexmo

client = nexmo.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)

View full source

Write the code

Add the following to cancel.py:

Copy to Clipboard
try:
    response = client.cancel_number({"country": COUNTRY_CODE, "msisdn": NEXMO_NUMBER})
    print("Number cancelled")
except Exception as exc:
    print("Error cancelling number", exc)

View full source

Run your code

Save this file to your machine and run it:

python cancel.py

Prerequisites

Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named cancel.rb and add the following code:

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET
)

View full source

Write the code

Add the following to cancel.rb:

Copy to Clipboard
begin
  response = client.numbers.cancel(
    country: COUNTRY_CODE,
    msisdn: NEXMO_NUMBER
  )
  puts "Number cancelled"
rescue
  puts "Error cancelling number"
end

View full source

Run your code

Save this file to your machine and run it:

ruby cancel.rb

See also