memcached delete command is used to delete an existing key from the memcached server.
syntax
the basic syntax of memcached delete command is as shown below −
delete key [noreply]
output
cas command may produce one of the following result −
deleted indicates successful deletion.
error indicates error while deleting data or wrong syntax.
not_found indicates that the key does not exist in the memcached server.
example
in this example, we use tutorialspoint as a key and store memcached in it with an expiration time of 900 seconds. after this, it deletes the stored key.
set tutorialspoint 0 900 9 memcached stored get tutorialspoint value tutorialspoint 0 9 memcached end delete tutorialspoint deleted get tutorialspoint end delete tutorialspoint not_found
delete data using java application
to delete data from a memcached server, you need to use the memcached delete method.
example
import java.net.inetsocketaddress;
import java.util.concurrent.future;
import net.spy.memcached.memcachedclient;
public class memcachedjava {
public static void main(string[] args) {
try{
// connecting to memcached server on localhost
memcachedclient mcc = new memcachedclient(new inetsocketaddress("127.0.0.1", 11211));
system.out.println("connection to server sucessful.");
// add data to memcached server
future fo = mcc.set("tutorialspoint", 900, "world's largest online tutorials library");
// print status of set method
system.out.println("set status:" + fo.get());
// retrieve and check the value from cache
system.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint"));
// try to add data with existing key
future fo = mcc.delete("tutorialspoint");
// print status of delete method
system.out.println("delete status:" + fo.get());
// retrieve and check the value from cache
system.out.println("tutorialspoint value in cache - " + mcc.get("codingground"));
// shutdowns the memcached client
mcc.shutdown();
}catch(exception ex)
system.out.println(ex.getmessage());
}
}
output
on compiling and executing the program, you get to see the following output −
connection to server successful set status:true tutorialspoint value in cache - world's largest online tutorials library delete status:true tutorialspoint value in cache - null