Deleted the stored data
You can either delete all the user's metadata, or ceetain fields from them:
Deleting specific fields from the metadata object#
This can be done by calling the update metadata function and setting the field you want to remove to be null. For example, if you have the following metadata object for a user:
{
"preferences": { "theme": "dark" },
"notifications": { "email": true },
"todos": ["use-text-notifs"]
}
And you want to remove the "notifications" field, you can update the metadata object with the following JSON:
{
"notifications": null
}
This would result in the final metadata object:
{
"preferences": { "theme": "dark" },
"todos": ["use-text-notifs"]
}
important
You can only remove the root level fields in the metadata object in this way. From the above example, if you set preferences.theme: null, then it will not remove the "theme" field, but instead set it to a JSON null value.
In code, it would look like:
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
- Express
- Hapi
- Fastify
- Koa
- Loopback
- AWS Lambda / Netlify
- Next.js
- NestJS
import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let app = express();
app.post("/updateinfo", verifySession(), async (req, res) => {
const session = req.session;
const userId = session.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
res.json({ message: "successfully updated user metadata" });
});
import Hapi from "@hapi/hapi";
import { verifySession } from "supertokens-node/recipe/session/framework/hapi";
import { SessionRequest } from "supertokens-node/framework/hapi";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let server = Hapi.server({ port: 8000 });
server.route({
path: "/updateinfo",
method: "post",
options: {
pre: [
{
method: verifySession(),
},
],
},
handler: async (req: SessionRequest, res) => {
const session = req.session;
const userId = session!.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
return res.response({ message: "successfully updated user metadata" }).code(200);
},
});
import Fastify from "fastify";
import { verifySession } from "supertokens-node/recipe/session/framework/fastify";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let fastify = Fastify();
fastify.post(
"/updateinfo",
{
preHandler: verifySession(),
},
async (req, res) => {
const session = req.session;
const userId = session.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
res.send({ message: "successfully updated user metadata" });
},
);
import { verifySession } from "supertokens-node/recipe/session/framework/awsLambda";
import { SessionEvent } from "supertokens-node/framework/awsLambda";
import UserMetadata from "supertokens-node/recipe/usermetadata";
async function updateinfo(awsEvent: SessionEvent) {
const session = awsEvent.session;
const userId = session!.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
return {
body: JSON.stringify({ message: "successfully updated user metadata" }),
statusCode: 200,
};
}
exports.handler = verifySession(updateinfo);
import KoaRouter from "koa-router";
import { verifySession } from "supertokens-node/recipe/session/framework/koa";
import { SessionContext } from "supertokens-node/framework/koa";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let router = new KoaRouter();
router.post("/updateinfo", verifySession(), async (ctx: SessionContext, next) => {
const session = ctx.session;
const userId = session!.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
ctx.body = { message: "successfully updated user metadata" };
});
import { inject, intercept } from "@loopback/core";
import { RestBindings, post, response } from "@loopback/rest";
import { verifySession } from "supertokens-node/recipe/session/framework/loopback";
import { SessionContext } from "supertokens-node/framework/loopback";
import UserMetadata from "supertokens-node/recipe/usermetadata";
class UpdateInfo {
constructor(@inject(RestBindings.Http.CONTEXT) private ctx: SessionContext) {}
@post("/updateinfo")
@intercept(verifySession())
@response(200)
async handler() {
const session = this.ctx.session;
const userId = session!.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
return { message: "successfully updated user metadata" };
}
}
import { superTokensNextWrapper } from "supertokens-node/nextjs";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
export default async function updateInfo(req: any, res: any) {
await superTokensNextWrapper(
async (next) => {
await verifySession()(req, res, next);
},
req,
res,
);
const session = (req as SessionRequest).session;
const userId = session!.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
res.json({ message: "successfully updated user metadata" });
}
import { Controller, Post, UseGuards, Session } from "@nestjs/common";
import { SessionContainer } from "supertokens-node/recipe/session";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { AuthGuard } from "./auth/auth.guard";
@Controller()
export class ExampleController {
// For more information about "AuthGuard" and the "Session" decorator please read our NestJS guide.
@Post("example")
@UseGuards(new AuthGuard())
async postExample(@Session() session: SessionContainer): Promise<{ message: string }> {
const userId = session.getUserId();
await UserMetadata.updateUserMetadata(userId, { notifications: null });
return { message: "successfully updated user metadata" };
}
}
import "github.com/supertokens/supertokens-golang/recipe/usermetadata"
func main() {
userId := "..."
usermetadata.UpdateUserMetadata(userId, map[string]interface{}{
"notifications": nil,
})
}
- Asyncio
- Syncio
from supertokens_python.recipe.usermetadata.asyncio import update_user_metadata
async def some_func():
user_id = "..."
await update_user_metadata(user_id, {
"notifications": None
})
from supertokens_python.recipe.usermetadata.syncio import update_user_metadata
user_id = "..."
update_user_metadata(user_id, {
"notifications": None
})
Deleting the entire metadata object#
Using this function will delete all the fields in the user metadata object for that user.
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
- Express
- Hapi
- Fastify
- Koa
- Loopback
- AWS Lambda / Netlify
- Next.js
- NestJS
import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let app = express();
app.post("/updateinfo", verifySession(), async (req, res) => {
const session = req.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
res.json({ success: true });
});
import Hapi from "@hapi/hapi";
import { verifySession } from "supertokens-node/recipe/session/framework/hapi";
import { SessionRequest } from "supertokens-node/framework/hapi";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let server = Hapi.server({ port: 8000 });
server.route({
path: "/updateinfo",
method: "post",
options: {
pre: [
{
method: verifySession(),
},
],
},
handler: async (req: SessionRequest, res) => {
const session = req.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
return res.response({ success: true }).code(200);
},
});
import Fastify from "fastify";
import { verifySession } from "supertokens-node/recipe/session/framework/fastify";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let fastify = Fastify();
fastify.post(
"/updateinfo",
{
preHandler: verifySession(),
},
async (req, res) => {
const session = req.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
res.send({ success: true });
},
);
import { verifySession } from "supertokens-node/recipe/session/framework/awsLambda";
import { SessionEvent } from "supertokens-node/framework/awsLambda";
import UserMetadata from "supertokens-node/recipe/usermetadata";
async function updateinfo(awsEvent: SessionEvent) {
const session = awsEvent.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
return {
body: JSON.stringify({ success: true }),
statusCode: 200,
};
}
exports.handler = verifySession(updateinfo);
import KoaRouter from "koa-router";
import { verifySession } from "supertokens-node/recipe/session/framework/koa";
import { SessionContext } from "supertokens-node/framework/koa";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let router = new KoaRouter();
router.post("/updateinfo", verifySession(), async (ctx: SessionContext, next) => {
const session = ctx.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
ctx.body = { success: true };
});
import { inject, intercept } from "@loopback/core";
import { RestBindings, post, response } from "@loopback/rest";
import { verifySession } from "supertokens-node/recipe/session/framework/loopback";
import { SessionContext } from "supertokens-node/framework/loopback";
import UserMetadata from "supertokens-node/recipe/usermetadata";
class UpdateInfo {
constructor(@inject(RestBindings.Http.CONTEXT) private ctx: SessionContext) {}
@post("/updateinfo")
@intercept(verifySession())
@response(200)
async handler() {
const session = this.ctx.session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
return { success: true };
}
}
import { superTokensNextWrapper } from "supertokens-node/nextjs";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
export default async function updateInfo(req: any, res: any) {
await superTokensNextWrapper(
async (next) => {
await verifySession()(req, res, next);
},
req,
res,
);
const session = (req as SessionRequest).session;
const userId = session!.getUserId();
await UserMetadata.clearUserMetadata(userId);
res.json({ success: true });
}
import { Controller, Post, UseGuards, Session } from "@nestjs/common";
import { SessionContainer } from "supertokens-node/recipe/session";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { AuthGuard } from "./auth/auth.guard";
@Controller()
export class ExampleController {
@Post("example")
@UseGuards(new AuthGuard())
async postExample(@Session() session: SessionContainer): Promise<{ success: boolean }> {
const userId = session.getUserId();
// For more information about "AuthGuard" and the "Session" decorator please read our NestJS guide.
await UserMetadata.clearUserMetadata(userId);
return { success: true };
}
}
import "github.com/supertokens/supertokens-golang/recipe/usermetadata"
func main() {
userId := "..."
usermetadata.ClearUserMetadata(userId)
}
- Asyncio
- Syncio
from supertokens_python.recipe.usermetadata.asyncio import clear_user_metadata
async def some_func():
user_id = "..."
await clear_user_metadata(user_id)
from supertokens_python.recipe.usermetadata.syncio import clear_user_metadata
user_id = "..."
clear_user_metadata(user_id)