User:Mumylulu123/Sandbox/API:Delete
| This page is part of the MediaWiki Action API documentation. |
POST Request to delete a page.
| MediaWiki version: | ≥ 1.12 |
API documentation
[edit]
action=delete(main | delete)
Delete a page. Specific parameters: Other general parameters are available.
Examples:
|
Example
[edit]The process has four steps:
- Fetch login token from API:Tokens.
- Send a POST request with the fetched login token and user information to the API.
- Retrieve a CSRF token when logged in.
- Send a post request to delete a page with the fetched CSRF token.
POST Request
[edit]Response
[edit]{
"delete": {
"title": "page name",
"reason": "content was: 'Test' and the only contributor was Username",
"logid": 1234567
}
}
Sample code
[edit]delete.py
#!/usr/bin/python3
"""
delete.py
MediaWiki Action API Code Samples
Demo of `Delete` module: post request to delete a page
MIT license
"""
import requests
S = requests.Session()
URL = "https://test.wikipedia.org/w/api.php"
# Step 1: Retrieve login token
PARAMS_0 = {
'action':"query",
'meta':"tokens",
'type':"login",
'format':"json"
}
R = S.get(url=URL, params=PARAMS_0)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
# Step 2: Send a post request to login. Use of main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
PARAMS_1 = {
'action':"login",
'lgname':"your_bot_username",
'lgpassword':"your_bot_password",
'lgtoken':LOGIN_TOKEN,
'format':"json"
}
R = S.post(URL, data=PARAMS_1)
# Step 3: When logged in, retrieve a CSRF token
PARAMS_2 = {
'action':"query",
'meta':"tokens",
'format':"json"
}
R = S.get(url=URL, params=PARAMS_2)
DATA = R.json()
CSRF_TOKEN = DATA['query']['tokens']['csrftoken']
# Step 4: Send a post request to delete a page
PARAMS_3 = {
'action':"delete",
'title':"enter_a_page_title",
'token':CSRF_TOKEN,
'format':"json"
}
R = S.post(URL, data=PARAMS_3)
DATA = R.json()
print(DATA)
Possible errors
[edit]| Code | Information |
|---|---|
| missingtitle | The page you specified doesn't exist. |
| notoken | The token parameter must be set. |
| badtoken | Invalid CSRF token. |
| permissiondenied | You don't have permission to delete this page. On most wikis, deleting pages is restricted to sysops, but other wikis may have stricter rules.
|
| cantdelete | The page or file "title" could not be deleted.
It may have already been deleted by someone else. |
Parameter History
[edit]- v1.27: Introduced
tags - v1.17: Deprecated
watch,unwatch - v1.14: Introduced
pageid - v1.13: Introduced
watch,oldimage,unwatch
Additional notes
[edit]- For older versions of MediaWiki, use edit token obtained via API:Tokens (action) or API:Info in place of login token.
- In addition to the
deleteright, other rights may be required, depending on the location and type of page. Deleting a user's.csspage, for example, also requires theeditusercssright.